缓存流的基本使用

缓冲流:其实就是个包装流,目的是起缓冲的作用,提升io 性能

下面以字节缓冲流为例,将传统与缓冲流进行对比,代码如下:

 1 public static void bufferedCopy(String src,String dest) throws IOException {
 2         long start = System.currentTimeMillis();
 3         FileInputStream in = new FileInputStream(src);
 4         FileOutputStream out = new FileOutputStream(dest);
 5         BufferedInputStream bufferIn = new BufferedInputStream(in);
 6         BufferedOutputStream bufferOut = new BufferedOutputStream(out);
 7         int len=-1;
 8         byte[] bytes = new byte[10];
 9         while((len=bufferIn.read(bytes))!=-1){
10             bufferOut.write(bytes,0,len);
11         }
12         System.out.println(System.currentTimeMillis()-start);
13         bufferIn.close();
14         bufferOut.close();
15         in.close();
16         out.close();
17     }
18  public static void copy(String src,String dest) throws IOException {
19         long start = System.currentTimeMillis();
20         FileInputStream in = new FileInputStream(src);
21         FileOutputStream out = new FileOutputStream(dest);
22         int len=-1;
23         byte[] bytes = new byte[10];
24         while((len=in.read(bytes))!=-1){
25             out.write(bytes,0,len);
26         }
27         System.out.println(System.currentTimeMillis()-start);
28         in.close();
29         out.close();
30  }
 先后调用copy,bufferedCopy方法,发现后者耗时更短
 
 
posted @ 2023-03-31 15:35  *屏风光芒无限亮  阅读(9)  评论(0)    收藏  举报