<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    轉(zhuǎn)載 java文件寫入耗時

    轉(zhuǎn)載地址:http://zhenqinghualy.blog.163.com/blog/static/5498053520100123332267/ 
    原創(chuàng)地址不詳 

    從老紫竹的網(wǎng)站上轉(zhuǎn)來滴 


    import java.io.BufferedOutputStream;   
    import java.io.File;   
    import java.io.FileOutputStream;   
    import java.io.FileWriter;   
    import java.nio.ByteBuffer;   
    import java.nio.channels.FileChannel;   
      
    /**  
    * 測試各種寫文件的方法性能。  
    *   
    * @author 老紫竹 JAVA世紀網(wǎng)(java2000.net)  
    *   
    */  
    public class T {   
      
      public static void main(String[] args) {   
        FileOutputStream out = null;   
        FileOutputStream outSTr = null;   
        BufferedOutputStream Buff = null;   
        FileWriter fw = null;   
        int count = 1000000;// 寫文件行數(shù)   
        try {   
          byte[] bs = "測試java 文件操作\r\n".getBytes();   
          out = new FileOutputStream(new File("C:/add.txt"));   
          long begin = System.currentTimeMillis();   
          for (int i = 0; i < count; i++) {   
            out.write(bs);   
          }   
          out.close();   
          long end = System.currentTimeMillis();   
          System.out.println("FileOutputStream執(zhí)行耗時:" + (end - begin) + " 豪秒");   
      
          outSTr = new FileOutputStream(new File("C:/add0.txt"));   
          Buff = new BufferedOutputStream(outSTr);   
          long begin0 = System.currentTimeMillis();   
          for (int i = 0; i < count; i++) {   
            Buff.write(bs);   
          }   
          Buff.flush();   
          Buff.close();   
          long end0 = System.currentTimeMillis();   
          System.out.println("BufferedOutputStream執(zhí)行耗時:" + (end0 - begin0) + " 豪秒");   
      
          fw = new FileWriter("C:/add2.txt");   
          long begin3 = System.currentTimeMillis();   
          for (int i = 0; i < count; i++) {   
            fw.write("測試java 文件操作\r\n");   
          }   
          fw.flush();   
          fw.close();   
          long end3 = System.currentTimeMillis();   
          System.out.println("FileWriter執(zhí)行耗時:" + (end3 - begin3) + " 豪秒");   
      
          long begin4 = System.currentTimeMillis();   
          String path = "C:/add3.txt";   
          ByteBuffer bb = ByteBuffer.wrap(bs);   
          FileChannel out2 = new FileOutputStream(path).getChannel();   
          for (int i = 0; i < count; i++) {   
            out2.write(bb);   
            bb.rewind();   
          }   
      
          out2.close();   
          long end4 = System.currentTimeMillis();   
          System.out.println("FileChannel執(zhí)行耗時:" + (end4 - begin4) + " 豪秒");   
        } catch (Exception e) {   
          e.printStackTrace();   
        } finally {   
          try {   
            fw.close();   
            Buff.close();   
            outSTr.close();   
            out.close();   
          } catch (Exception e) {   
            e.printStackTrace();   
          }   
        }   
      }   
    }  

    在我的筆記本上,運行結(jié)果如下 

    FileOutputStream執(zhí)行耗時:4891 豪秒 

    BufferedOutputStream執(zhí)行耗時:78 豪秒 

    FileWriter執(zhí)行耗時:438 豪秒 

    FileChannel執(zhí)行耗時:2812 豪秒 

    當然最終生成的文件都是正確的 

    總結(jié): 

    BufferedOutputStream 由于有緩沖,性能明顯好 

    FileOutputStream 性能最差,因為其每次都寫入字節(jié)。 

    FileWriter 性能很一般 

    FileChannel使用了nio,但如果沒有緩沖,必能太指望性能了 

    posted on 2013-12-25 14:24 葉落心成 閱讀(475) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    <2013年12月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    導航

    統(tǒng)計

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 欧洲 亚洲 国产图片综合| 黄网站色视频免费看无下截 | 亚洲人成网站18禁止久久影院 | 最近2022中文字幕免费视频| 亚洲大尺码专区影院| 永久免费看bbb| 国产免费牲交视频免费播放| 亚洲精品在线观看视频| 妞干网免费观看视频| 久青草视频在线观看免费| 亚洲人成黄网在线观看| 亚洲中文字幕丝袜制服一区| 免费视频爱爱太爽了| 特级av毛片免费观看| 亚洲综合激情六月婷婷在线观看| 国产禁女女网站免费看| 99视频在线精品免费| 美女羞羞喷液视频免费| 亚洲精品456在线播放| 亚洲第一黄片大全| 日韩国产免费一区二区三区| 一级日本高清视频免费观看 | 亚洲AV永久无码精品| 国产无遮挡吃胸膜奶免费看视频| 久久国产精品国产自线拍免费| 亚洲人av高清无码| 亚洲精品美女视频| 在线日韩日本国产亚洲| 女人被弄到高潮的免费视频| 久久精品中文字幕免费| 成人免费网站久久久| 一本色道久久88亚洲精品综合| 亚洲av日韩综合一区在线观看| 免费国产小视频在线观看| 免费看国产成年无码AV片| A级毛片高清免费视频在线播放| 日韩在线视精品在亚洲| 亚洲人成在线精品| 亚洲国产成人久久综合一 | 亚洲精品高清国产麻豆专区| 亚洲精品乱码久久久久久|