Introduction and use of BufferedReader and BufferedWriter in Java

BufferedReader and BufferedWriter are character input and output streams with default buffers, which are more efficient than no buffers:

      1. The java.io.BufferedReader and java.io.BufferedWriter classes each have a buffer of 8192 characters. When BufferedReader reads a text file, it will first try to read character data from the file and put it into the buffer, and then if the read() method is used, it will first read from the buffer. If the buffer data is insufficient, it will be read from the file. When using BufferedWriter, the written data will not be output to the destination first, but will be stored in the buffer first. If the data in the buffer is full, the destination will be written out at one time.

      2. When the user input is directly read from the standard input stream System.in, System.in reads a character every time the user inputs a character. In order to read the user's input one line at a time, a BufferedReader is used to buffer the characters entered by the user. The readLine() method will pass in the entire line string again when the user's newline character is read.

    3. System.in is a bit stream. In order to convert it into a character stream, InputStreamReader can be used to perform character conversion for it, and then BufferedReader can be used to add buffering function to it.

A BufferedReader class

Constructor: BufferedReader br = new BufferReader(Reader in);

Main method: int read();//Read a single character.

            int read(char[] cbuf,int off,int len);//Read characters into a part of the array. Returns the number of characters read. When the tail is reached, return -1.

                String readLine(); //Read a text line.

                void close(); //Close the stream. and release all resources associated with that stream.

The usage example is as follows:

FileInputStream inputstream = new FileInputStream(System.in);
StringBuffer buffer = new StringBuffer();
String line; // used to save the content of each line read
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(inputstream));
line = bufferreader.readLine(); // read the first line
while (line != null) { // if line is empty it means finished
	buffer.append(line); // Append the read content to the buffer
	buffer.append("\n"); // add a newline
	line = bufferreader.readLine(); // read next line
}
inputstream.close(); //Write the content read into the buffer
System.out.print(buffer) ;

Two BufferedWriter classes

Constructor: bufferedWriter bf = new bufferedWriter(Writer out );

Main method: void write(char ch);//Write a single character.

                void write(char []cbuf,int off,int len)//Write a part of character data.

                void write(String s,int off,int len)//Write a part of the string.

                void newLine()//Write a line separator.

                void flush();//Flush the buffer in the stream. Write the buffered data to the destination file.

                void close();//Close this stream, it will be refreshed before closing.

The usage example is as follows:

import java.io.BufferedWriter;  
import java.io.FileWriter;  
import java.io.IOException;

public class BufferedWriterDemo {
      public static void main(String[] args) throws IOException {
            FileWriter fw = new FileWriter("Buffered.txt");
            fw.write("Hello Hello");  
            fw.close();  
            BufferedWriter bfw= new BufferedWriter(fw); //Use the character stream buffer to improve efficiency
            bfw.write("hello hello fast"); //Use the buffer method to write data to the buffer
            bfw.newLine();  
            bfw.write("hello hello world");  
            bfw.flush(); //Use the method in the buffer to flush the data to the destination file
            bfw.close();//Close the buffer and close the fw stream object at the same time
      }  
}  

Comprehensive application example:

The following procedure mainly accomplishes copying content from one document to another.

import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;  
  
public class DocTransfer {  
     public static void main(String[] args) throws IOException {  
          FileReader fr = new FileReader("D:\\doc1.txt");  
          FileWriter fw = new FileWriter("D:\\doc2.txt");  
          BufferedReader bfr = new BufferedReader(fr);  
          BufferedWriter bfw = new BufferedWriter(fw);    
          String line = null;  
          while((line = bfr.readLine()) != null){  
                bfw.write(line);//Write line by line  
                bfw.newLine();  
                bfw.flush();  
          }
          /*int ch = 0;
          while((ch = bfr.read())!=-1){
                bfw.write(ch);
          }//Write word by word*/  
          bfr.close();  
          bfw.close();  
    }  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906313&siteId=291194637