Caching can be said to be a performance optimization for I/O. The cache stream adds a memory buffer to the I/O stream. With buffers, it is possible to execute the skip () \mark () and Reset () methods on the stream.
Outline:
BufferedReader class and Bufferwriter class
The BufferedReader class and the Bufferwriter class inherit the reader class and the writer class respectively. These two classes also have an internal caching mechanism, and can be input and output in the unit behavior units.
According to the characteristics of the BufferedReader class, the process of reading a file from character data can be summed up: character data = = "buffereawriter==" outoutstreamwriter== "outoutstream==" file.
The common methods of the BufferedReader class are as follows:
Read () Method: reads a single character.
ReadLine () Method: reads a line of text and returns it as a string, or null if no data is readable.
Write (string s,int off,int len) method: Writes a portion of a string.
Flush () Method: Refreshes the cache of the process.
NewLine () Method: writes a row separator.
When using the Write () method of the BufferedWriter class, the data is not written directly into the output stream, but first into the buffer, if you want to immediately write the buffer data to the output stream, be sure to call the Flush () method.
ImportJava.io.*; Public classTestFile4 { Public Static voidMain (string[] args) {Try{File F=NewFile ("D:\\test.txt"); //with Cache//the implementation class of writer interfaceFileWriter fw=NewFileWriter (F,true); //Cache write classes that need to be passed into writer instances when constructedBufferedWriter bw=NewBufferedWriter (FW); Bw.write ("\ n This is the cached way to write the string"); //automatically manage caching//Auto Write: 1. Cache is full 2. Before the cache is closedBw.flush ();//actively emptying the cache, writing dataBw.write ("\ n This is the string that was written after the cache was emptied."); Bw.close (); Fw.close (); System.out.println ("Write Complete"); //Cache ReadFileReader FR=NewFileReader (f); BufferedReader BR=NewBufferedReader (FR); //first time Read//String str=br.readline ();// //System.out.println (str);// //While (Str!=null)// {//System.out.println (str);// //str=br.readline ();// } while(true) {String str=Br.readline (); if(str==NULL) { Break; } System.out.println (str); } } Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); } }}
View Code
Character input and output stream with cache