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

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

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

    轉載 java文件寫入耗時

    轉載地址:http://zhenqinghualy.blog.163.com/blog/static/5498053520100123332267/ 
    原創地址不詳 

    從老紫竹的網站上轉來滴 


    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世紀網(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;// 寫文件行數   
        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執行耗時:" + (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執行耗時:" + (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執行耗時:" + (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執行耗時:" + (end4 - begin4) + " 豪秒");   
        } catch (Exception e) {   
          e.printStackTrace();   
        } finally {   
          try {   
            fw.close();   
            Buff.close();   
            outSTr.close();   
            out.close();   
          } catch (Exception e) {   
            e.printStackTrace();   
          }   
        }   
      }   
    }  

    在我的筆記本上,運行結果如下 

    FileOutputStream執行耗時:4891 豪秒 

    BufferedOutputStream執行耗時:78 豪秒 

    FileWriter執行耗時:438 豪秒 

    FileChannel執行耗時:2812 豪秒 

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

    總結: 

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

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

    FileWriter 性能很一般 

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

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


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


    網站導航:
     
    <2013年12月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费无码精品黄AV电影| 全部在线播放免费毛片| 毛片无码免费无码播放| 国产精品亚洲A∨天堂不卡| 黄色网页在线免费观看| 国产偷国产偷亚洲清高动态图| 黄人成a动漫片免费网站| 免费国产a国产片高清网站| 337P日本欧洲亚洲大胆精品| 日韩视频免费一区二区三区| 美女黄色免费网站| 亚洲AV成人潮喷综合网| 久久www免费人成精品香蕉| 黑人大战亚洲人精品一区| 免费看黄的成人APP| 亚洲视频在线一区| 4399好看日本在线电影免费| 亚洲真人无码永久在线观看| 精品久久久久久久免费人妻| 国产亚洲女在线线精品| 2048亚洲精品国产| 久久黄色免费网站| 亚洲香蕉久久一区二区三区四区| 免费鲁丝片一级在线观看| 色多多www视频在线观看免费| 亚洲另类激情综合偷自拍图| 亚洲免费视频网站| 亚洲综合激情五月丁香六月| 无码专区一va亚洲v专区在线 | 精品一区二区三区免费视频| 日本亚洲视频在线| 欧洲一级毛片免费| 亚洲av乱码一区二区三区按摩| 国产精品亚洲综合专区片高清久久久| 美女视频黄a视频全免费网站色窝 美女被cao网站免费看在线看 | 日韩免费视频在线观看| 一区二区三区免费电影| 亚洲丁香色婷婷综合欲色啪| 免费观看美女裸体网站| 黄色网站软件app在线观看免费 | 亚洲国产精品SSS在线观看AV|