The decorative design pattern is used: This class is designed to improve the efficiency of flow operation data. The idea is to define the container to store the data temporarily, and in the case of the buffer object, the container is actually packed and provides a more efficient method of operation.
Principle
1, the Read method of the stream is used to fetch a batch of data from the source stored in the array of buffers.
2, the number of stored elements is recorded by the counter.
3, get the elements in the array (fetch data from the buffer) by the array's Corner label.
4, the pointer will continue to increment, when added to the array length, will be 0. The counter is self-decreasing, and when it is reduced to 0 o'clock, a batch of data is taken from the source into the buffer.
Bufferreader:
PackageCom.lp.ecjtu;ImportJava.io.BufferedReader;ImportJava.io.FileReader;Importjava.io.IOException; Public classBufferreaderdemo {/** * @paramargs *@throwsIOException * * This buffer goes to provide a read-one-line ReadLine () method that reads to the end when NULL is returned*/ Public Static voidMain (string[] args)throwsIOException {//Create a read stream object and file associatedFileReader FR =NewFileReader ("Filereaderdemo.java"); //in order to improve the efficiency of character reading, buffer technology is added//The method of constructing a stream object that needs to be increased efficiency as a parameter into the buffer isBufferedReader bw =NewBufferedReader (FR); String Line=NULL; /*Line = Bw.readline (); SYSTEM.OUT.PRINTLN ("******" +line);//One-time reading of one row of String line1 = Bw.readline (); SYSTEM.OUT.PRINTLN ("******" +line1); String line2 = Bw.readline (); SYSTEM.OUT.PRINTLN ("******" +line2);*/ while((Line=bw.readline ())! =NULL) {System.out.println (line); } bw.close (); }}
Bufferwriter:
PackageCom.lp.ecjtu;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;Importjava.io.IOException; Public classBufferwriterdemo {/** * @paramargs *@throwsIOException * * This buffer provides a cross-platform line break newline method*/ Public Static voidMain (string[] args)throwsIOException {//creates a character to write to a stream objectFileWriter FW =NewFileWriter ("Buf.txt"); //in order to improve the efficiency of character writing, buffer technology is added//The method of constructing a stream object that needs to be increased efficiency as a parameter into the buffer isBufferedWriter BW =NewBufferedWriter (FW); for(inti=0;i<5;i++) {Bw.write ("ABCD" +i); Bw.newline (); Bw.flush (); } }}
Copy the contents of one text into another file through a buffer:
PackageCom.lp.ecjtu;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classCopytextbybuffer {/** * @paramThe args * ReadLine () method returns the contents of the data before the carriage return, does not return carriage returns, and can be wrapped through the newline () method .*/ Public Static voidMain (string[] args) {BufferedReader bufr=NULL; BufferedWriter BUFW=NULL; Try{BUFR=NewBufferedReader (NewFileReader ("Filereaderdemo.java")); BUFW=NewBufferedWriter (NewFileWriter ("BufferWriterCopy.txt")); String Line=NULL; while((Line=bufr.readline ())! =NULL) {bufw.write (line); Bufw.newline (); Bufw.flush (); } } Catch(FileNotFoundException e) {Throw NewRuntimeException ("The specified file was not found! Please make sure the file name is correct! "); }Catch(IOException e) {Throw NewRuntimeException ("Read and Write Failed!")); }finally{ Try { if(Bufr! =NULL) {bufr.close (); } } Catch(IOException e) {Throw NewRuntimeException ("Read shutdown failed!")); } Try { if(BUFW! =NULL) {bufw.close (); } } Catch(IOException e) {Throw NewRuntimeException ("Write shutdown failed!")); } } }}
Java Basics Review of Javaio---BufferedReader and bufferedwriter