Java中的IO的性能优化

 

Java中的IO的性能优化
在使用IO的时候注意一下细节,能使性能得到很大的优化.
首先读写大文件,使用Buffer是肯定的了,使用方法,有下边两个

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

12 ExpandedSubBlockStart.gifContractedSubBlock.gif    }
  catch  (IOException e)  dot.gif {
13 ExpandedSubBlockEnd.gif    }

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

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

29 InBlock.gif        fis.close();
30 ExpandedSubBlockStart.gifContractedSubBlock.gif    }
  catch  (IOException e)  dot.gif {
31 ExpandedSubBlockEnd.gif    }

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


方法一1937ms 
方法二47ms
测试结果相差大概50倍左有,很大。
而我平时的习惯是方法一。问题主要出在readLine()上。

如果只是简单的文件拷贝,下边两种方法也不错

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

 8 ExpandedBlockEnd.gif}

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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值