DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          Java中的IO的性能優化

          ?

          Java中的IO的性能優化
          在使用IO的時候注意一下細節,能使性能得到很大的優化.
          首先讀寫大文件,使用Buffer是肯定的了,使用方法,有下邊兩個

          ?1 方法一:
          ?2 public ? static ? void ?test1(String?fileName)? {
          ?3 ???? long ?start? = ?System.currentTimeMillis();
          ?4 ???? try ? {
          ?5 ????????FileInputStream?fis? = ? new ?FileInputStream(fileName);
          ?6 ????????BufferedReader?br? = ? new ?BufferedReader( new ?InputStreamReader(fis)?);
          ?7 ????????StringBuffer?sb? = new ?StringBuffer();
          ?8 ????????String?str;
          ?9 ???????? while ?((str? = ?br.readLine())? != ? null )? {
          10 ????????????sb.append(str);
          11 ????????}

          12 ????}
          ? catch ?(IOException?e)? {
          13 ????}

          14 ???? long ?end? = ?System.currentTimeMillis();
          15 ???? long ?time = end - start;
          16 ????System.out.println(time);
          17 }

          18 方法二:
          19 public ? static ? void ?test2(String?fileName)? {
          20 ???? long ?start? = ?System.currentTimeMillis();
          21 ???? try ? {
          22 ????????FileInputStream?fis? = ? new ?FileInputStream(fileName);
          23 ???????? byte ?buf[]? = ? new ? byte [ 8192 ];
          24 ???????? int ?n;
          25 ????????StringBuffer?sb? = ? new ?StringBuffer();
          26 ???????? while ?((n? = ?fis.read(buf))? != ? - 1 )? {
          27 ????????????sb.append(buf);
          28 ????????}

          29 ????????fis.close();
          30 ????}
          ? catch ?(IOException?e)? {
          31 ????}

          32 ???? long ?end? = ?System.currentTimeMillis();
          33 ???? long ?time = end - start;
          34 ????System.out.println(time);
          35 }


          方法一1937ms?
          方法二47ms
          測試結果相差大概50倍左有,很大。
          而我平時的習慣是方法一。問題主要出在readLine()上。

          如果只是簡單的文件拷貝,下邊兩種方法也不錯

          ?1 public ????? static ????? void ???test3()?????? {
          ?2 ????String?cmd?? = ? " ?copy?d:/out1.txt?out2.txt? " ?;
          ?3 ???? try ? {
          ?4 ?????????Runtime.getRuntime().exec(cmd);
          ?5 ????}
          ? catch ?(Exception?e)? {
          ?6 ?????????e.printStackTrace();
          ?7 ????}

          ?8 }

          ?9 public ??? static ??? void ??test4()? {
          10 ????String?inFileName?? = ??? " ?d:/out1.txt? " ?;
          11 ????String?outFileName?? = ??? " ?d:/out2.txt? " ?;
          12 ???? long ??start?? = ??System.currentTimeMillis();
          13 ???? try ? {
          14 ????????File?inFile?? = ??? new ??File(inFileName);
          15 ????????File?outFile?? = ??? new ??File(outFileName);????????
          16 ????????RandomAccessFile?inRaf?? = ??? new ??RandomAccessFile(inFile,? " ?r? " ?);
          17 ????????RandomAccessFile?outRaf?? = ??? new ??RandomAccessFile(outFile,? " ?rw? " ?);?????????????
          18 ????????FileChannel?infc?? = ??inRaf.getChannel();
          19 ????????FileChannel?outfc?? = ??outRaf.getChannel();?????????????
          20 ????????infc.transferTo(? 0 ?,?inFile.length(),outfc?);?????????????
          21 ????????infc.close();
          22 ????????outfc.close();
          23 ????}
          ? catch ?(Exception?e)? {
          24 ????????e.printStackTrace();
          25 ????}

          26 ???? long ??end?? = ??System.currentTimeMillis();
          27 ???? long ??time? = ?end? - ?start;
          28 ????System.out.println(time);
          29 }



          posted on 2006-11-10 18:05 dreamstone 閱讀(2200) 評論(3)  編輯  收藏 所屬分類: jdk相關

          評論

          # re: Java中的IO的性能優化 2006-11-10 20:49 翔 ruben

          你讀一行和讀byte [ 8192 ]本來就不公平。
          新IO優勢在非阻塞!不過個人比較喜歡新IO。
          不過舊IO真的很簡單,直觀!
          還有編碼轉化,個人覺得這個才真的令人煩!  回復  更多評論   

          # re: Java中的IO的性能優化 2006-11-27 16:12 roygbip

          方法一如果用來讀取大文件就會出現OutOfMemoryError,所以以后還是用第二個方法好了。  回復  更多評論   

          # re: Java中的IO的性能優化 2006-11-27 19:01 dreamstone

          讀取大文件都不行的,這個只是體現問題的demo,如果要讀取大文件須要重寫很多,比如加大buffer,讀一次寫一次,或者直接用channel  回復  更多評論   

          主站蜘蛛池模板: 宜章县| 环江| 抚顺县| 封丘县| 南郑县| 鄂尔多斯市| 崇仁县| 沂南县| 广州市| 济南市| 上林县| 抚州市| 焉耆| 临漳县| 介休市| 习水县| 莒南县| 镇坪县| 通城县| 涞水县| 竹山县| 门源| 会昌县| 芒康县| 堆龙德庆县| 钟祥市| 岗巴县| 永登县| 会泽县| 芮城县| 武安市| 娄烦县| 侯马市| 崇文区| 湟源县| 达州市| 长泰县| 仪征市| 彭水| 文昌市| 即墨市|