Bufferedinputstream & Bufferedoutputstream
As described above, in the Bufferedinputstream constructor you need to pass in a inputstream, bufferedinputstream inside a byte array buffer, each time you perform a read operation, the data is read from this buf , there is not much overhead in reading the data from BUF. If the data to be read is not already in buf, then the Read method of the InputStream with its internal bindings is executed, and a large chunk of data is read at once to fill the buf buffer. The default size of the buffer buf is 8192 bytes, which is 8K, in the constructor we can also pass in a size to specify the buffer. As we perform the Bufferedinputstream read operation, it is very often the data that is read from the buffer, which greatly reduces the number of read operations that are actually performed on the specified InputStream, and increases the efficiency of the reads. The opposite of Bufferedinputstream is bufferedoutputstream. In Bufferedoutputstream's constructor we need to pass in a outputstream, which binds bufferedoutputstream to the outputstream. Bufferedoutputstream inside a byte buffer buf, when performing a write operation, the data to be written is first cached together, stored in a byte buffer buf, BUF is a limited size, the default size is 8192 bytes, that is, 8KB, Of course, you can also pass in the constructor to specify the size of the BUF. The BUF as long as the size has been specified will not automatically expand, so it is a limited size, since there is a limited size, there will be filled with the moment, when the buf is filled, will call Bufferedoutputstream Flushbuffer method, The method uses the Write method of its bound outputstream to perform the actual write operation of the data in BUF and points the buf to zero (which can be seen as emptying the data in buf). If you want to make the data in the buffer buf really written to OutputStream, you can call the Flush method, and the Flushbuffer method is called inside the flush method. Because of the existence of BUF, it will greatly reduce the number of write operations that actually perform outputstream, and optimize the efficiency of writing.
The following are sample code snippets for Bufferedinputstream and Bufferedoutputstream:
Private Static voidTestbufferedinputoutputstream () {Try{String InputFileName="D:\\iwork\\file1.txt"; String OutputFileName="D:\\iwork\\file2.txt"; FileInputStream FIS=NewFileInputStream (InputFileName); Bufferedinputstream bis=NewBufferedinputstream (FIS,1024x768*Ten); FileOutputStream Fos=NewFileOutputStream (OutputFileName); Bufferedoutputstream Bos=NewBufferedoutputstream (FOS,1024x768*Ten); byte[] buf =New byte[1024x768]; intLength =0; while(length = Bis.read (BUF)) >0) {bos.write (buf,0, length); } bis.close (); Bos.close (); }Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); } }
- The above code reads the file input stream from File1.txt and writes the read data to the File2.txt, which enables the file1.txt to be copied to File2.txt. In fact not through Bufferedinputstream and Bufferedoutputstream can also accomplish such a job, the advantage of using this two class is that It can improve the efficiency of file1.txt reading and file2.txt writing, thus improving the efficiency of file copy.
Common IO streams and their use in Java