字节流FileInputStream FileOutputStream四种读写,包装流BufferedOutputStream BufferedInputSream 缓冲区默认8192字节

 1 package test03;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 public class Streamtest {
10     public static void main(String[] args) throws IOException {
11         long start = System.currentTimeMillis();
12         //test1();//2421
13         //test2();//8
14 //        test3();//52
15         test4();//6
16         System.out.println(System.currentTimeMillis()-start);
17     }
18     //基本字节流,一次读写一个字节
19     public static  void test1() throws IOException {
20         //封装源文件
21         FileInputStream fis = new  FileInputStream("G:/Lighthouse.jpg");
22         //封装目标文件
23         FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse01.jpg");
24         //读写
25         int b = 0;
26         while((b = fis.read())!=-1) {
27             fos.write(b);
28         }
29         //关闭资源
30         fis.close();
31         fos.close();
32     }
33     //基本字节流   一次一个字节数组
34     public static void test2() throws IOException {
35         FileInputStream fis = new FileInputStream("G:/Lighthouse.jpg");
36         FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse02.jpg");
37         
38         //读写
39         int len =0;
40         byte[] bys = new byte[1024];
41         while((len = fis.read(bys))!=-1) {
42             fos.write(bys);
43         }
44         //关闭资源
45         fis.close();
46         fos.close();
47     }
48     //缓冲字节流 一次一个字节
49     public static void test3() throws IOException {
50         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg"));
51         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse03.jpg"));
52         //读写
53         int b = 0;
54         while((b = bis.read())!=-1) {
55             bos.write(b);
56         }
57         bis.close();
58         bos.close();
59         
60     }
61     //缓冲字节流 一次一个字节数组
62     public static void test4() throws IOException {
63         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("G:/Lighthouse.jpg"));
64         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("G:/demo/lighthouse04.jpg"));
65         //读写
66         int len = 0;
67         byte[] bys = new byte [1024];
68         while((len = bis.read(bys))!=-1) {
69             bos.write(bys);
70         }
71         bis.close();
72         bos.close();
73         
74                 
75         
76     }
77     
78 }
字节流四种读写方式

原文地址:https://www.cnblogs.com/star521/p/8931567.html