On the Usage of Buffer Character Stream BufferedReader BufferedWriter

  • 2021-11-02 00:45:45
  • OfStack

Buffer character streams BufferedReader and BufferedWriter

Question:

Previous file reading and writing are in accordance with bytes, characters or arrays to achieve, for text files, can you read and write in accordance with lines, 1 line.

BufferedReader and BufferedWriter are provided to realize line-by-line reading and writing


package com.bjsxt.ios3;
import java.io.*;
public class TestBufferedReaderWriter {
    public static void main(String[] args) throws IOException {
        //1. Create a stream 
        BufferedReader br =
                new BufferedReader(new FileReader("c:/RecorderSDKLog.txt"));
        BufferedWriter bw =
                new BufferedWriter(new FileWriter("c:/RecorderSDKLog2.txt"));

        //2. Using streams 
        String str = br.readLine();
        while(str != null){
            // Output the current row data 
            System.out.println(str);
            // Write the current line of data to a file 
            bw.write(str);
            //bw.write("\n");
            bw.newLine();
            // Reread 1 Row 
            str = br.readLine();
        }
        //3. Close flow 
        br.close();
        bw.close();
    }
}

Summary 1: Advantages of BufferedReader and BufferedWriter

STEP 1 Fast

2. Simplify programming

Summary 2: The underlying principle of readLine ()

The bottom layer is also a 1-character read. append () is put into StringBuilder (or char []). If a newline character is encountered, StringBuilder (char []) is converted to String and returned

Summary 3: Line breaks are different in different operating systems

In the Unix system, each line ends with only " < Line break > ", that is,"\ n ";

In the Windows system, each line ends with " < Carriage return > < Line break > ", that is,"\ r\ n ";

In the Mac system, each line ends with " < Carriage return > ", that is,"\ r ".

Reasons for better performance of BufferedReader and BufferedWriter

Principle:

BufferedReader will read 8k (default value, can be set) bytes from the physical stream to memory once. If there is a request from the outside world, it will be accessed here. If there is no memory, it will be read again in the physical stream. Even if you read it, it is 8k.

Read the physical stream directly, and read it by bytes.

Every read of the physical stream has an IO operation. The IO operation is the most time consuming.

BufferedReader just reduces a lot of IO operations and saves you time.

Simply put, one IO operation, reading one byte is also reading, and reading 8k bytes is also reading, which takes about the same time. However, it takes a lot of time to operate IO back and forth once.

It's like a large car (equipped with 100 people). If you want to go to the station to pick up people to the company, you will pick up one person and pick up 100 people, and the time will be the same. Obviously, picking up 100 people is the best deal.

The physical stream is 1 byte at a time (1 person)

Buffered is 8k bytes at a time (100 people)

For reading fixed-length byte files, of course BufferedReader is faster!


Related articles: