首页 > 代码库 > IO中关于自定义缓冲区和使用默认缓冲区哪个效率更高的对比

IO中关于自定义缓冲区和使用默认缓冲区哪个效率更高的对比

//自己测试了一个3.8G的文件,有兴趣的可以自己试试看,初学java写的不对的地方希望大家能指出,有疑问可以留言一起探讨,谢谢!
 1 package cn.String.Day.IO;
 2 
 3 import java.io.*;
 4 
 5 /**
 6  * Created by Void on 2017/6/20.
 7  */
 8         public class copyInputOutput {
 9             public static void main(String[] args) throws IOException {
10                 bufferTime();
11                 imbufferTime();
12 
13             }
14     private static void imbufferTime() throws IOException {
15         FileInputStream fileInputStream = new FileInputStream("D:\\360安全浏览器下载\\win7中文旗舰版64位系统.iso");
16         FileOutputStream fileOutputStream = new FileOutputStream("e:\\win7旗舰版64bit.iso");
17         long startTime = System.currentTimeMillis() ;
18         byte[] by = new byte[4096];
19         int len = 0;
20         while ((len = fileInputStream.read(by))!=-1){
21             fileOutputStream.write(by,0,len);
22 
23         }
24         fileOutputStream.close();
25         fileInputStream.close();
26         long endTime = System.currentTimeMillis();
27         System.out.println("自定义缓冲花费的时间是:"+(endTime-startTime)/1000);
28     }
29 
30     private static void bufferTime() throws IOException {
31 
32         BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\360安全浏览器下载\\win7中文旗舰版64位系统.iso"),4096);
33 
34         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("e:\\win7旗舰版64bit.iso"),4096);
35         long startTime = System.currentTimeMillis() ;
36         int len = 0;
37         while ((len = bufferedInputStream.read())!=-1){
38             bufferedOutputStream.write(len);
39 
40         }
41         bufferedOutputStream.close();
42         bufferedInputStream.close();
43         long endTime = System.currentTimeMillis();
44         System.out.println("原缓冲花费的时间是:"+(endTime-startTime)/1000);
45     }
46 }

 

IO中关于自定义缓冲区和使用默认缓冲区哪个效率更高的对比