带缓存的输入流输出流

时间:2023-02-24 21:33:58
 1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 public class TestBufferedStream {
9 public static void main(String[] args) {
10 BufferedInputStream bis = null;
11 BufferedOutputStream bos = null;
12
13 try {
14 bis = new BufferedInputStream(new FileInputStream("D:\\MY文档\\pic\\云纱.jpg"));
15 bos = new BufferedOutputStream(new FileOutputStream("C:/Users/Polly/java/天河菱纱.jpg"));
16 byte[] buf = new byte[1024];
17 int len = 0;
18 while((len=bis.read(buf))>=0){
19 bos.write(buf,0,len);
20 }
21 //需要对缓存流进行刷新,或者用finally结束缓存流;
22 //bos.flush();
23
24 } catch (FileNotFoundException e) {
25 e.printStackTrace();
26 } catch (IOException e) {
27 e.printStackTrace();
28 } finally{
29 try {
30 if (bis!=null) bis.close();
31 } catch (IOException e) {
32 e.printStackTrace();
33 }
34 try {
35 if (bos!=null) bos.close();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40
41 }
42
43 }