This is the 11th day of my participation in Gwen Challenge

Accumulate over a long period, constant dripping wears away a stone 😄

preface

When reading text files using a byte stream, one minor problem is that when Chinese characters are encountered, the full character may not be displayed because a Chinese character may take up more than one byte. So Java provides character stream classes that read and write data in character units, specifically for processing text files.

Here’s what happens when you use byte streams to read text files.

Create a new file c.txt with the content: Hello ABC

public static void main(String[] args) throws Exception {
    // create FileInputStream and bind the data source to read
    FileInputStream fr = new FileInputStream(\ \ "IO flow c.t xt." ");
    //2. Use read in FileInputStream to read the file
    int len = 0;
    while((len = fr.read()) ! = -1) {
        System.out.println(len + "= = = = >"+ (char)len);
    }
    //3. Release resources
    fr.close();
}
Copy the code

You can see that Chinese is garbled. A Chinese character occupies two bytes in GBK encoding and three bytes in UTF-8 encoding.

1, character input stream

Java.io.Reader This abstract class is a superclass that represents all the classes used to read character streams, which can read character information into memory. It defines the basic public methods for character input streams.

  • Pulic void close() : Closes this stream and releases any system resources associated with it.
  • Public int read() : Reads a character from the input stream.
  • Public int read(char[] cbuf) : Reads some characters from the input stream and stores them in the character array cbuf.

1.1, FileReader【 file character input stream 】

Java. IO. FileReader extends InputStreamReader.

Java. IO. InputStreamReader extends the Reader.

I’ll come back to the InputStreamReader class.

FileReader: Reads data from a hard disk file into memory as characters.

1.1.1. Construction method

  • FileReader(String fileName) : Creates a new oneFileReaderGiven the name of the file to read.
  • FileReader(File File) : Creates a new oneFileReader, given the File object to read

When you create a stream object, you must pass in a file path. If the file does not exist in this path, FileNotFoundException will be thrown. This approach is similar to FileInputStream.

For example, the code is as follows:

public static void main(String[] args) {
    // Create a stream object with the File object
    File file = new File("a.txt");
    FileReader fr = new FileReader(file);
    // Create a stream object with the file name
    FileReader fr2 = new FileReader("b.txt");
}
Copy the code

The function of the construction method:

  • 1. Create oneFileReaderobject
  • 2, theFileReaderThe object points to the file to read

1.1.2 reading character data

  • Reading a single character:read()Method, which can read data one character at a time to the end of the file, returns -1.

Code usage demo:

 public static void main(String[] args) throws Exception {
        // create a FileReader object with the constructor bound to the data source to be read
        // c.txt file content is still hello ABCD
        FileReader fr = new FileReader(\ \ "IO flow c.t xt." ");
        //2. Read the file using the read in the FileReader object
        int len = 0;
        while((len = fr.read()) ! = -1) {
            System.out.println(len + "= = = = >"+ (char)len);
        }
        //3. Release resources
        fr.close();
    }
Copy the code

You can see the results! The difference between this sample code and the previous code is thatFileInputStreamObject modified toFileReaderObject.

  • 2. Use character array to read::read(char[] b)Each time b is read into the array, return the number of valid characters read, at the end of the read, return -1.

Code usage demo:

// Reads the character array
public static void main(String[] args) throws Exception {
    // create a FileReader object with the constructor bound to the data source to be read
    FileReader fr = new FileReader(\ \ "IO flow c.t xt." ");
    //2. Read the file using the read in the FileReader object
   char[] b = new char[2];
    int len = 0;
    while((len = fr.read(b)) ! = -1) {
        System.out.println(len + "= = = = >"+ new String(b));
    }
    //3. Release resources
    fr.close();
}
Copy the code

See the extra C character in the result. Use the String(byte[] bytes) constructor to get a valid character.

public static void main(String[] args) throws Exception {
    // create a FileReader object with the constructor bound to the data source to be read
    FileReader fr = new FileReader(\ \ "IO flow c.t xt." ");
    //2. Read the file using the read in the FileReader object
   char[] b = new char[2];
    int len = 0;
    while((len = fr.read(b)) ! = -1) {
        System.out.println(len + "= = = = >"+ new String(b,0,len));
    }
    //3. Release resources
    fr.close();
}
Copy the code

  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.