Java Foundation - Byte Buffered Stream of IO Stream Objects (BufferedOutputStream and BufferedInputStream)

          Java Foundation - Byte Buffered Stream of IO Stream Objects (BufferedOutputStream and BufferedInputStream)

                                       Author: Yin Zhengjie

Copyright statement: original works, please do not reprint! Otherwise held liable.

 

  When we learn byte stream and character stream, everyone has done the operation of reading the data in the file. When reading a file with a large amount of data, the reading speed will be very slow, which will greatly affect the efficiency of our program. Then, I want to increase the speed, what should I do? A set of buffer streams has been improved in Java, and its existence can improve the read and write speed of IO streams

  Buffered streams are classified into byte buffered streams and character buffered streams according to stream classification. This blog mainly introduces the byte buffer stream.

 

 

1. Byte buffer stream

  There are 2 byte buffer streams according to the direction of the stream:

      1>. Write data to the stream, byte buffered output stream BufferedOutputStream

      2>. Read the data in the stream, byte buffer input stream BufferedInputStream

  They all contain a buffer inside. By reading and writing in the buffer, the reading and writing speed of the IO stream can be improved.

 

2. Byte output buffer stream

  The role of java.io.BufferedOutputStream: Improve the writing efficiency of the original output stream.

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedOutputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 
13 public class BufferedOutputStreamDemo {
14     public static void main(String[] args) throws IOException {
15          // Create a byte output stream and bind the file 
16          FileOutputStream fos = new FileOutputStream("yinzhengjie.txt" );
 17          // Create a byte output stream buffer stream object, in the construction method, pass the byte output stream, here It can also be generated directly anonymously. 
18          BufferedOutputStream bos = new BufferedOutputStream(fos);    
 19          // write a byte 
20          bos.write(97 );
 21          // turn the string into a byte array 
22          byte [] buf = "yinzhengjie" .getBytes () ;     
 23          // write a byte array 
24          bos.write(buf);
 25          // write part of the byte array 
26         bos.write(buf, 3, 5 );
 27          // Release resources, we don't need to close the fos object, because closing the bos object will automatically close the fos stream. 
28          bos.close();
 29      }
 30 }

 

3. Byte input buffer stream

  The role of java.io.BufferedInputStream: Improve the reading efficiency of the original input stream.

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedInputStream;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 
13 public class BufferedInputStreamDemo {
14     public static void main(String[] args) throws IOException {
15          // Create a byte input stream and wrap the file 
16          FileInputStream fis = new FileInputStream("yinzhengjie.txt" );
 17          // Create a byte input stream buffer stream object, wrap the byte input stream in the constructor, and wrap the file 
18          BufferedInputStream bis = new BufferedInputStream(fis);
 19          // The read operation is performed as a byte array, here I do not read as a single byte. 
20          byte [] buf = new  byte [4096 ];
 21          int len;
 22          while (( len = bis.read(buf)) != -1 ) {
 23              System.out.println( new String(buf,0 ,len ));
24          }
 25          // Release resources, here it is still not necessary to close the fis stream object. 
26          bis.close();
 27      }
 28  }
 29  
30  
31  /* 
The execution result of the above code in
 32 is as follows:  33 ayinzhengjiezheng
 34 */  

 

4. Efficiency comparison of four file copying methods

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.note5;
 8 
 9 import java.io.BufferedInputStream;
10 import java.io.BufferedOutputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 
16 public class CopyFile {
17     public static void main(String[] args) throws IOException {
18         
19         long start = System.currentTimeMillis();
20 //        copy1(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));
21 //        copy2(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));
22 //        copy3(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));      //
23         copy4(new File("yinzhengjie.sql"),new File("yinzhengjie.sql.backup"));   //// It only takes 862 milliseconds to copy a 600M file, and the efficiency is the highest! (SSD) 
24          long stop = System.currentTimeMillis();
 25          System.out.println(stop - start);
 26      }
 27      
28      
29  
30      // 1>. Byte stream reads and writes a single character. 
31      public  static  void copy1(File src,File dest) throws IOException{
 32          FileInputStream fis = new FileInputStream(src);
 33          FileOutputStream fos = new FileOutputStream(dest);
 34          int len;
 35          while( (len = fis.read()) != -1) {
36             fos.write(len);
37         }
38         fos.close();
39         fis.close();
40     }
41     
42     //2>.字节流读写字节数组
43     public static void copy2(File src,File dest) throws IOException{
44         FileInputStream fis = new FileInputStream(src);
45         FileOutputStream fos = new FileOutputStream(dest);
46         byte[] cache = new byte[1024];
47          int len ​​= 0 ;
 48          while ( (len = fis.read(cache)) != -1 ) {
 49              fos.write(cache, 0 , len);
 50          }
 51          fos.close();
 52          fis.close ();
 53      }
 54      
55      // Read a single character from the byte stream buffer 
56      public  static  void copy3(File src,File dest) throws IOException{
 57          BufferedInputStream bis = new BufferedInputStream( new FileInputStream(src));
 58         BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(dest));
 59          
60          int len ​​= 0 ;
 61          while ( (len = bis.read()) != -1 ) {
 62              bos.write(len);
 63          }
 64          bos .close();
 65          bis.close();
 66      }
 67      
68      // byte stream buffer read character array 
69      public  static  void copy4(File src,File dest) throws IOException{
 70          BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
71         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
72         byte[] cache = new byte[1024];
73         int len;
74         while((len = bis.read(cache)) != -1 ) {
75             bos.write(cache, 0, len);
76         }
77     }
78     
79 }

 

Guess you like

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