<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 葉落心成 閱讀(482) 評論(0)  編輯  收藏


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


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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人免费网站视频www| 色费女人18女人毛片免费视频| 亚洲视频在线播放| 亚洲高清中文字幕| 久章草在线精品视频免费观看| 亚洲中文字幕成人在线| 美女又黄又免费的视频| 国产99视频精品免费视频7| 美女扒开尿口给男人爽免费视频| 免费一级一片一毛片| 亚洲AV永久无码精品一百度影院| 国内精品免费久久影院| 亚洲AV无码乱码在线观看富二代| 国产婷婷成人久久Av免费高清| 无码少妇一区二区浪潮免费| 免费人成在线观看网站品爱网日本| 成人精品国产亚洲欧洲| 国产成人亚洲精品影院 | 亚洲国产成人久久一区二区三区| 在人线av无码免费高潮喷水| 性色av极品无码专区亚洲| 免费网站看av片| 亚洲校园春色小说| 色www永久免费视频| 人人爽人人爽人人片av免费| 亚洲AV永久精品爱情岛论坛| 黄色免费网站网址| 91大神亚洲影视在线| 手机看黄av免费网址| 国产亚洲精品2021自在线| 天天摸夜夜摸成人免费视频| 免费观看四虎精品成人| 久久精品亚洲中文字幕无码网站 | 九月丁香婷婷亚洲综合色| 精品日韩99亚洲的在线发布| 久久国产乱子伦精品免费看| 亚洲国产成人精品久久| 国产一级理论免费版| 久久免费区一区二区三波多野| 亚洲伊人久久大香线蕉AV| 久久亚洲国产成人影院网站 |