Copy of files, buffers of byte streams, BufferedInputStream class

Hello everyone, I am a Java advanced.

Foreword

This article mainly learns the copy of the document, the buffer of byte stream, the BufferedInputStream class. Read the file If a one is read and written, such operational file efficiency is too low, read the data of multiple bytes through a byte array through a buffer to learn the byte array, one-time data of the byte array. Write in the file. Next, Xiaobian took everyone to learn!

First, the copy of the file

1. The copy of the file is to read the file by the input stream, and write the file by the output stream. How to copy files? Use the FileInputStream class with the FileoutPutStream class copy file.

2. First create a TEST06.TXT file under the Text folder, and write content as “Java advanced learning exchange 001, Java advanced learning exchange 003”, as shown in the following figure:

3. The copy of the code implementation file is as follows:

importjava.io *;. publicclassP06 {publicstaticvoidmain (String [] args) throwsException {// TODOAuto-generatedmethodstub // Create an input stream of bytes read text data folder test06.txt InputStreamin = newFileInputStream ( “text / test06” ); // Create a byte output stream to read the data and create a TEST07.txt file outputstreamout = newfileoutputstream (“text / test07”); intNum; // Define the INT type Variable Num Save the system time longstime = system.currenttime = system.currenttimemillis () (// While cyclic judgment before reading) One byte of the read file is read. Read ())! = – 1) {// Read the data written in the file Out.write (NUM);} // Reproducing the system time LongendTime = system.currentTimeMillis (); system.out.println “The time used by the replication file is:” + (endtime-starttime) + “milliseconds”); // Turn off the stream in .close (); out.close ();}}

The result of the operation is as shown below:

Second, the buffer of byte stream

1. When the file is implemented, if one byte is read and written, it is often too low. First create a byte array to save data from multiple bytes at once, and then write the data in the byte array once. The buffer is quite byte arrays!

2. How to use the buffer copy file?

(1) First create a TEST08.txt file under the Text folder, and write content “HelloWorld, Java program design 002!, HelloWorld, Java program design 003!, HelloWorld, Java program Design 004!, HelloWorld, Java Program Design 005!, HelloWorld, Java Program Design 006! “, As shown below:

(2) The code is as follows:

importjava.io *;. publicclassP07 {publicstaticvoidmain (String [] args) throwsException {// TODOAuto-generatedmethodstub // Create an input stream of bytes read text data folder test08.txt InputStreamin = newFileInputStream ( “text / test08” ); // Create a byte output stream to read data and create a TEST09.txt file under the Text folder OutputStreamout = NewFileoutputStream (“text / test09); // Define byte arrays Use the cache area read and write Document Data Bytebt [] = newbyte [1024]; // Defines a variable NUM of an int type, saving the number of bytes INTNUM read to the buffer; // The system time before // before replicating the file longstime = system.currentTimeMillis (); // While loop judges whether the read byte reads the end of the file while ((Num = in.read (bt))! = – 1) {Out.write (BT, 0, NUM);} // Copy file The post-system time longendtime = system.currentTimeMillis (); system.out.println (“The time used by the copy file is:” + (endtime-starttime) + “ms”); // Close the stream in. Close (); Out.close ();}} The result of the run is shown below:

The above code mainly implements the copy of the TEST08.TXT file, and the WHILE loop is used in the copy process, and each cycle reads a lot of bytes in the TEST08.txt file to the array, through the variable Num Save the number of bytes of the read array, starting from the first byte of the array, writing NUM bytes to the file, when the NUM value is -1, the read file is over. Finally, the copy between the files is the buffer through the word stream.

Third, BufferedInputStream class

1. BufferedInputStream is a buffer input stream that reduces the number of access to the disk, and improves the read performance of the file, which is a subclass of the FilterInputStream class.

2. BufferedInputStream effect It is a buffer array with respect to the ordinary input. Each time you call the read () method, first read the data from the buffer, if you read the data failure, read the new data from the file. The buffer is displayed again.

3. How to read the text content with the BufferedInputStream class?

(1) Define a byte array BT, and then circulate the text content through the loop to the BT, then display the read data.

(2) First of all the TEXT folders Create a Test10.txt file and write the contents of “Java Advanced Learning Exchange 001!”.

(3) Implementation of the code:

importjava.io *;. publicclassP08 {publicstaticvoidmain (String [] args) throwsException {// TODOAuto-generatedmethodstub // Create an input buffer with a flow BufferedInputStreambi = newBufferedInputStream (newFileInputStream ( “text / test10”)); // define Byte array Byte [] bt = newbyte [1024]; intNum = 0; // Judgment whether you read the end of the file while ((Num = bi.read (bt))! = – 1) {// Read the word The section is converted to string object strings = newstring (bt, 0, num); system.out.println (“The content read is: + s);} // Close stream bi.close ();}}

The result of the operation is as shown below:

Fourth, summary

This article mainly introduces the copy of the file, the buffer of byte streams, the BufferedInputStream class.The copy of the file is the input stream to read the data of the file, and the data is written to the file by the output stream.The buffer of the byte stream creates a byte array to save the data of multiple bytes at once, and then write the data in the byte array.BufferedInputStream is a buffer input stream that reduces the number of access to the disk, enhances the read performance of the file, which is a subclass of the FilterInputStream class; it has a buffer array, call the read () method each time, first read data from the bufferIf the read data fails, the new data is read from the file to the buffer, and then display the contents of the buffer.I hope to help you through this article! This article reprinted from the WeChat public number “Java advanced learning exchange”, you can pay attention to the following QR code.Reprint this article Please contact Java advanced learning exchange public account.