One, character stream BufferedReader:BufferedReader reads the contents from the buffer, and all input byte data is placed in the bufferBufferedWriter: Writes a batch of data to a buffer, when the buffer area is full, and then writes the contents of the buffer to the character output stream
Second, read and write to the text file 2.1, character input stream
2.2. Character output stream
2.3. Comprehensive use
Packagecom.pb.io.buffered;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classBuferetest {/*BufferedWriter * 1. Needs to be modified to a specified output stream (for example: FileWriter) or other * 2.BufferedWriter overwrites the original file contents * 3. Need to clear the buffer, use the flush () method * BufferedReader * 1. Needs to be modified to a specified input stream (for example: FileReader) or other * 2. Close the stream*/ Public Static voidMain (string[] args) {/** BufferedWriter write Operation*/ Try { //1. Create a file output stream FileWriterFileWriter fw=NewFileWriter ("D:/test/test.txt"); //There is no plus true, is the empty file is re-written and can be added to True//2. Instantiate. BufferedWriter Object wrapper classBufferedWriter bw=NewBufferedWriter (FW); //3. Writing contentBw.write ("Hello, everyone!")); Bw.write ("I'm going to change the content."); Bw.newline (); //line BreakBw.write ("Haha, I'm done!")); Bw.newline (); //4. Emptying the bufferBw.flush (); //5. Close the streamBw.close (); Fw.close (); System.out.println ("========== Write file End ======"); } Catch(IOException e) {e.printstacktrace (); } /** BufferedReader Read file*/ Try { //1. Declaring file read ObjectsFileReader fr=NewFileReader ("D:/test/test.txt"); //2. Declaring Bufferedread objectsBufferedReader br=NewBufferedReader (FR); //3. Start reading filesSystem.out.println ("======== start reading file ========"); String date; while((Date=br.readline ())! =NULL) {System.out.println (date); } System.out.println ("======== Read file End ========"); //4. Close the streamBr.close (); Fr.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Results:
========== Write file End ============== start reading file ======== Hello everyone ! I'm going to change the content. !======== Read the end of the file ========
Three, byte-character conversion stream
- OutputStreamWriter: is a writer's subclass that transforms the output character into a byte stream, that is, the output object of a character stream becomes a byte stream output object.
- InputStreamReader: is a subclass of reader that converts the input byte stream into a character stream, that is, the input object of a stream of bytes into the input object of a character stream.
FileOutputStream is a direct subclass of OutputStream, and FileInputStream is also a direct subclass of InputStream,
but in the character stream file of the two operation classes are some special, filewriter is not directly the writer's subclass, but the OutputStreamWriter subclass,
and FileReader is not directly a subclass of reader, is a subclass of Inputstreamrader,
then the inheritance of these two classes can be clearly found, whether the use of a byte stream or a character stream is actually ultimately in the form of bytes operation of the input and output stream. Example 1. Implementing a copy of a text file
S1.txt content when the moon has, wine ask the sky, do not know the Sky Palace, I ask is what year.
Packagecom.pb.io.buffered;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;/** Implementation of text File replication * using BufferedReader and bufferedwriter to achieve*/ Public classBuffereDemo1 { Public Static voidMain (string[] args) {Try { //1. Declaring BufferedReader and BufferedWriter objectsInputStreamReader isr=NewInputStreamReader (NewFileInputStream ("D:/test/s1.txt"), "GB2312");//solve Chinese garbled characters//filereader fr=new filereader ("D:/test/s1.txt"); BufferedReader br=NewBufferedReader (ISR); OutputStreamWriter OSW=NewOutputStreamWriter (NewFileOutputStream ("D:/test/s2.txt"), "GB2312"); //FileWriter fw=new FileWriter ("D:/test/s2.txt");//S2 does not exist, it is automatically createdBufferedWriter BW =NewBufferedWriter (OSW); //2. Start reading filesString Line;//declaring variables used to hold read-out content intNum=1;//used to record the number of rows while((Line=br.readline ())! =NULL) {System.out.println ("========= is reading out the" +num+ "line ==========="); System.out.println (line); System.out.println ("========= is writing the" +num+ "line ==========="); Bw.write (line); Num++; } System.out.println ("====== File Write End ========"); //emptying buffersBw.flush (); //turn off the output streamBw.close (); Osw.close (); System.out.println ("====== file read End ========"); //close the input streamBr.close (); Isr.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }}
Java starts from scratch 36 (Java io-character stream)