5、过滤流

 1 package Io;
 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 public class BufferedStream_IoTest {
12     public static void main(String[] args) {
13         long startTime = new Date().getTime();
14         FileInputStream fis = null;
15         FileOutputStream fos = null;
16         BufferedInputStream bis = null;
17         BufferedOutputStream bos = null;
18         try {
19             fis = new FileInputStream("C:\\Users\\Samuel\\Pictures\\Allah Will Listen.png");
20             bis = new BufferedInputStream(fis);
21             fos = new FileOutputStream("G:\\IoTest\\Allah Will Listen.png");
22             bos = new BufferedOutputStream(fos);
23             byte[] buf = new byte[1024];
24             int len = 0;
25             while ((len = fis.read(buf)) > 0) {
26                 bos.write(buf, 0, len);
27             }
28 
29         } catch (FileNotFoundException e) {
30             e.printStackTrace();
31         } catch (IOException e) {
32             e.printStackTrace();
33         } finally {
34             //当关闭流时会自动flush
35             if (fis != null)
36                 try {
37                     fis.close();
38                 } catch (IOException e) {
39                     // TODO Auto-generated catch block
40                     e.printStackTrace();
41                 }
42         }
43         long endTime = new Date().getTime();
44         System.out.println("需要"+(endTime-startTime)/1000);
45     }
46 }

 

posted @ 2015-12-03 20:14  Akke  阅读(172)  评论(0编辑  收藏  举报