[Java] 过滤流BufferedInputStream和BufferedOutputStream

 1 package test.stream;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.util.Date;
10 
11 /**
12  * 过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读写的效率。
13  * @author Frost.Yen
14  * @E-mail 871979853@qq.com
15  * @date 2016年4月13日
16  */
17 public class TestBufferedSteam01 {
18     public static void main(String[] args) {
19         long startTime = new Date().getTime();
20         FileInputStream fis = null;
21         BufferedInputStream bis = null;
22         BufferedOutputStream bos = null;
23         try {
24             fis = new FileInputStream("D:\\FrostYen\\software\\Adobe\\FlashBuilder_4_7_LS10.exe");
25             bis = new BufferedInputStream(fis);
26             bos = new BufferedOutputStream(new FileOutputStream("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\c.exe"));
27             byte[] buf = new byte[1024];
28             int len = 0;
29             while((len = bis.read(buf))>=0){
30                 bos.write(buf, 0, len);
31             }
32             //bos.flush();当关闭流 的时候自动flush
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         }finally {
38             //当关闭流 的时候自动flush
39             if(bis!=null)
40                 try {
41                     bis.close();
42                 } catch (IOException e) {
43                     e.printStackTrace();
44                 }
45             if(bos!=null)
46                 try {
47                     bos.close();
48                 } catch (IOException e) {
49                     e.printStackTrace();
50                 }
51         }
52         
53         long endTime = new Date().getTime();
54         //拷贝所耗时间,测试性能
55         System.out.println((endTime-startTime)/1000);
56     }
57 }

 

posted on 2016-04-13 13:57  晏过留痕  阅读(585)  评论(0编辑  收藏  举报