Special methods for character buffer streams:
A, Bufferedwriter:
public void NewLine (): based on system to determine line break
1 Private Static voidWrite ()throwsIOException {2 //creating a character output buffer stream3BufferedWriter BW =NewBufferedWriter (NewFileWriter ("bw.txt"));4 for(intx = 0;x < 10; X + +){5Bw.write ("java" +x);6 //automatically wrap every time.7 Bw.newline ();8 }9 //Freeing ResourcesTen bw.close (); one}
B, Bufferedreader:
Public String ReadLine (): reads one row of data at a time
note: A string that contains the contents of the row, does not contain any line terminators, and returns null if the end of the stream has been reached
1 Private Static voidRead ()throwsIOException {2 //creating a character input buffer stream3BufferedReader br =NewBufferedReader (NewFileReader ("bw.txt"));4 //read one row at a time5 //with a loop, the terminating condition is to return null6String str =NULL;//reading is a string7 while(str = Br.readline ())! =NULL){8System.out.println (str);//Print out the contents of the Bw.txt.9 }Ten //Freeing Resources one br.close (); a}
Use character buffer streams to copy text files: be familiar with
public Static voidMain (string[] Args)throwsIOException {//Encapsulating Data SourcesBufferedReader br =NewBufferedReader (NewFileReader ("a.txt")); //Package DestinationBufferedWriter BW =NewBufferedWriter (NewFileWriter ("copy.txt")); //copy in a newline-read mannerString len =NULL; while(len = Br.readline ())! =NULL){ //output to the copy file if there is contentBw.write (len); //Note that this is going to be a wrap, or the content will be Connected.Bw.newline (); } //Freeing ResourcesBr.close (); Bw.close (); }
Java 21-6 character Buffer stream special method and the method to efficiently copy files