In the previous sections we learned about file byte input and output streams and file character input and output streams. In this section we learn a new concept - buffered streams . So what is a buffered stream? What role can buffer streams play?
In Java, we call the objects created by the BufferedReader and BufferedWriter classes buffered input and output streams, which enhance the ability to read and write files. For example, Student.txt is a list of students, with each name occupying one line. If we want to read the name, we must read one line at a time. It is difficult to complete such a task using the FileReader stream, because we do not know how many characters there are in a line, and the FileReader class does not provide a method to read a line.
Java provides more advanced streams: BufferedReader stream and BufferedWriter stream. The source and destination of both must be character input stream and character output stream. Therefore, if the file character input stream is used as the source of the BufferedReader stream, and the file character output stream is used as the destination of the BufferedWriter stream, then the streams created by the BufferedReader and BufferedWriter classes will have stronger reading and writing capabilities than the character input stream and character output stream. ability. For example, a BufferedReader stream can read files line by line.
The constructors of the BufferedReader class and BufferedWriter are:
BufferedReader(Readerin);BufferedWriter(Writerout);
BufferedReader streams can read lines of text using readLine() . Create a BufferedReader object by passing an object of a Reader subclass, such as an instance of FileReader, to BufferedReader, for example:
FileReaderinOne=newFileReader(Student.txt);BufferedReaderinTwo=BufferedReader(inOne);
Then inTwo stream calls the readLine() method to read Student.txt, for example:
StringstrLine=inTwo.readLine();
Similarly, you can connect a BufferedWriter stream and a FileWriter stream together, and then use the BufferedWriter stream to write data to the destination, for example:
FileWritertofile=newFileWriter(hello.txt);BufferedWriterout=BufferedWriter(tofile);
Then out uses the method write(String s, int off, int len) of the BufferedReader class to write the string s to hello.txt. The parameter off is the offset from the beginning of s, and len is the number of characters written.
In addition, the BufferedWriter stream has a unique method of writing a newline character to the file:
newLine();
BufferedReader and BufferedWriter can be called the upper stream, and the character stream they point to is called the underlying stream. Java uses caching technology to connect the upper stream and the underlying stream. The underlying character input stream first reads the data into the cache, the BufferedReader stream then reads the data from the cache; the BufferedWriter stream writes the data into the cache, and the underlying character output stream continuously writes the data in the cache to the destination. When the BufferedWriter stream calls flush() to refresh the cache or calls the close() method to close, even if the cache does not overflow, the underlying stream will immediately write the cached content to the destination.
Note : When closing the output stream, you must first close the buffered output stream, and then close the stream pointed to by the buffered output stream, that is, first close the upper stream and then close the lower stream. Just close the upper stream when writing code, and the underlying stream of the upper stream will be closed automatically.
For example:
The file english.txt composed of English sentences is as follows, each sentence occupies one line:
Thearrowmissedthetarget.Theyrejectedtheuniondemand.Wheredoesthisroadgoto?
It is required to read english.txt line by line, add the number of words contained in the English sentence after the line, and then write the line to a file named englishCount.txt. The code is as follows:
importjava.io.*;importjava.util.*;publicclassMain{publicstaticvoidmain(Stringargs[]){FilefRead=newFile(english.txt);FilefWrite=newFile(englishCount.txt);try{Writerout=newFileWriter(fWrite);BufferedWriterbufferWrite= newBufferedWriter(out);Readerin=newFileReader(fRead);BufferedReaderbufferRead=newBufferedReader(in);Stringstr=null;while((str=bufferRead.readLine())!=null){StringTokenizerfenxi=newStringTokenizer(str);intcount=fenxi. countTokens();str=str+number of words in the sentence:+count;bufferWrite.write(str);bufferWrite.newLine();}bufferWrite.close();out.close();in=newFileReader(fWrite);bufferRead =newBufferedReader(in);Strings=null;System.out.println(fWrite.getName()+content:);while((s=bufferRead.readLine())!=null){System.out.println(s) ;}bufferRead.close();in.close();}catch(IOExceptione){System.out.println(e.toString());}}}
The running results are as follows:
englishCount.txt content:Thearrowmissedthetarget.The number of words in the sentence:5Theyrejectedtheuniondemand.The number of words in the sentence:5Wheredoesthisroadgoto?The number of words in the sentence:6