Buffer stream (character type) to copy text files [Java]

Posted by greenber on Mon, 03 Jan 2022 18:59:11 +0100

Buffer stream (character type) realizes the copy of text file

  • Using buffered stream can improve the speed of file reading

When the character buffer stream reads data, it can read text line by line

  • That is, use the readLine() method in the BufferedReader class

    • readLine();

      • If there is content in this line, the content of this line will be read and returned as a String object
      • If there is no content in this line, a null is returned at this time

      Note: this method reads one line of data at a time, but the data read with the readLine() method does not contain line feed operations - at this time, when we write the data read with the readLine() method, we will find that one line has been written out

      • To solve this problem, we need to add line breaks manually
        1. Add "\ n" after the data written out each time;
        2. After each data is written, the newLine() method is called.

String type data can be written out when character buffer stream writes out data:

  • At this time, if the data we want to write out is read by using the readLine() method, we need to manually add "\ n" after the String type data written out each time, or call the newLine() method

A buffer is provided in the buffer stream

  • If it is a byte buffer stream, a buffer of 8192 bytes is provided
  • If it is a character buffer stream, a buffer of 8192 characters is provided
    • Our buffer provides a temporary storage function

eg: Here we give an example: - use this example to master the use of character buffer stream to copy text files

Use BufferedReader and BufferedWrite to copy text files

  • Here we use char [] as the buffer:

package IO flow.Buffered;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) throws IOException{
        /*
        Create a buffer flow object (here we directly pass the node flow object into the buffer flow object construction method in the form of anonymous object)
         */
        BufferedReader br = new BufferedReader(new FileReader(new File("Hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Hello3.txt")));

        /*
        Create buffer char []
         */
        char [] chars = new char[1024];
        
        /*
        Create a temporary variable -- this temporary variable is used to store the return value of the read() method
         */
        int len = 0;
        /*
        Read and write file data
         */
        while((len = br.read(chars)) != -1){
            bw.write(chars,0,len);
        }

        /*
        Close stream resource
         */
        bw.close();
        br.close();
    }
}
  • Here, we handle the exception in the way of throws + exception type

    • Here we want to make the regulations clearer - but we should use the try – catch – finally method to handle exceptions in actual programming, and put the closure of our stream resources into the finally code block
  • Here we use string as the buffer:

package IO flow.Buffered;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) throws IOException{
        /*
        Create a buffer flow object (here we directly pass the node flow object into the buffer flow object construction method in the form of anonymous object)
         */
        BufferedReader br = new BufferedReader(new FileReader(new File("Hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Hello3.txt")));

        /*
        Create a temporary variable to temporarily store every read data
         */
        String data = null;
        while((data = br.readLine()) != null){
            /*
            Since we use the readLine() method to read data one line at a time, we do not read line breaks when reading data
            So at this time, when we write, we will write the original data in one line. In order to restore the file to the original file
            As like as two peas, we will give a new line to the end of each output, or we will use BufferedWriter.
            Class to add a new line

            In fact, when we call the newLine() method, we do a line feed
             */


            /*
            This is how we use to manually wrap the written data every time
             */
//            bw.write(data + "\n");

            /*
            Here, we call the newLine() method to wrap lines
             */
            bw.write(data);
            bw.newLine();
        }
        /*
        Close stream resource
         */
        bw.close();
        br.close();
    }
}

Topics: Java Back-end