In the examples of FileInputStream and FileOutputStream, a byte array is used as a buffer for data read-in, for example, for file access, the speed of hard disk access is much lower than in-memory data access speed. In order to reduce access to the hard disk, it is common to read a certain length of data from the file, and write a certain length of data at a time, which can increase the efficiency of file access.
Java.io.BufferedInputStream and Java.io.BufferedOutputStream can increase the buffer function for objects of the InputStream, OutputStream class.
When building a Bufferedinputstream instance, you need to give an instance of the InputStream type, and finally implement the InputStream instance when Bufferedinputstream is implemented.
Similarly, when building bufferedoutputstream, it is also necessary to give a OutputStream instance when implementing Bufferedoutputstream, in fact, the OutputStream instance is actually implemented.
The Bufferedinputstream data member buf is a bit array, which defaults to 2048 bytes. When reading a data source, such as a file, Bufferedinputstream tries to fill the buf as much as possible. When using the Read () method, the data in the BUF is actually read first rather than directly to the source of the data. When the data in the BUF is insufficient, Bufferedinputstream will then implement the read () method of the given InputStream object, extracting the data from the specified appliance.
The Bufferedoutputstream data member buf is a bit array, which defaults to 512 bytes. When writing data using the Write () method, the data is actually written to buf, and the Write () method of the given OutputStream object is implemented when the BUF is full, and the BUF data is written to the destination instead of being written to the destination each time.
--------------Call the Bufferedinputstream, bufferedoutputstream example------------------
1 Public Static voidMain (string[] args) {2 Try {3Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream ("F:/a.mp3"));4Bufferedoutputstream bos=NewBufferedoutputstream (NewFileOutputStream ("F:/b.mp3"));5 byte[] b=New byte[1024];//①..?? Why do we use arrays6 intLen=0;7 while( -1!= (len=Bis.read (b))) {8Bos.write (b,0, Len);9 }Ten One}Catch(FileNotFoundException e) { ASYSTEM.OUT.PRINTLN ("File not Found"); - e.printstacktrace (); -}Catch(IOException e) { the e.printstacktrace (); -}finally{ - if(NULL!=Bos) { - bos.close (); + } - if(NULL!=bis) { + bis.close (); A } at } -}
①. Here, I have a question: Since Bufferedinputstream has been buffered, why write that byte[] array in the program to improve performance? Personal understanding: (to be discussed) 1. They do not produce the same: the buffer array in Bufferedinputstream is not the same as the buffer array that you define. The buffer array in the Bufferedinputstream is already defined at the beginning of the program, saving time for the program to create an array. The byte[] B array in the above code, which is equivalent to the buffer array, was created only when the program was run and took some time 2. They do not have the same object: the Bufferedinputstream buffer array is the face of the program on the hard disk Data, while its own in the program-defined array byte[] B is the data that has been read from the hard disk into memory, that is, byte[] B is the data that reads the BUF array in memory. 3. Their purpose is consistent: they are all designed to improve performance
Understanding of the implementation principles of Bufferedinputstream and Bufferedoutputstream