Wrapper stream BufferedOutputStream BufferedInputSream Buffer default 8192 bytes

 

 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      // Basic byte stream, read and write one byte at a time 
19      public  static   void test1() throws IOException {
 20          // Encapsulate the source file 
21          FileInputStream fis = new   FileInputStream("G:/Lighthouse.jpg" );
 22          // Encapsulate the target file 
twenty three         FileOutputStream fos = new FileOutputStream("G:/demo/lighthouse01.jpg" );
 24          // read and write 
25          int b = 0 ;
 26          while ((b = fis.read())!=-1 ) {
 27              fos. write(b);
 28          }
 29          // close the resource 
30          fis.close();
 31          fos.close();
 32      }
 33      // basic byte stream one byte array at a time 
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     // Buffered byte stream one byte at a time 
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          // read and write 
53          int b = 0 ;
 54          while ((b = bis.read())!=-1 ) {
 55              bos.write(b) ;
 56         }
 57          bis.close();
 58          bos.close();
 59          
60      }
 61      // Buffer byte stream one byte array at a time 
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          // read and write 
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 }
Four ways to read and write byte streams

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782372&siteId=291194637