The character stream is optimized for the characteristics of character data.Thus providing some useful character-oriented features,The source or destination of a character stream is usually a text file. reader and writer are the parent classes for all character streams in the java.io package.Since they are all abstract classes,So you should use their subclasses to create entity objects,Use objects to handle related read and write operations.The subclasses of reader and writer can be divided into two categories:one is used to read data from a data source or write data to a destination (called a node stream), and the other performs some processing on the data (called Process stream).

The character-oriented input stream classes are all subclasses of reader, and their class hierarchy is shown in the figure.

The following table lists the main subclasses and descriptions of readers.

The reader provides the method shown in this table.You can use these methods to get bit data within a stream:

Reading files using the filereader class

The filereader class is a subclass of the reader subclass of the inputstreamreader class,So the filereader class can use either the reader class method or the inputstreamreader class method to create the object.

When reading a file using the filereader class,You must first call the filereader () constructor to create an object of class filereader.Then call the read () method. The format of the filereader constructor is:

 public filereader (string name);//Create a readable input stream object based on the file name

[Example] Use the filereader class to read the contents of a plain text file

import java.io. *;
class ep10_1 {
  public static void main (string args []) throws ioexception {
    char a []=new char [1000];//Create an array that can hold 1000 characters
    filereader b=new filereader ("ep10_1.txt");
    int num=b.read (a);//Read data into array a and return the number of characters
    string str=new string (a, 0, num);//Convert string array to string
    system.out.println ("The number of characters read is:" + num + ", the content is:\ n");
    system.out.println (str);

have to be aware of is,java treats a kanji or english letter as a character,A carriage return or line feed is treated as two characters.

Reading files using the bufferedreader class

The bufferedreader class is used to read the data in the buffer.When used, you must create a filereader class object, and then create an object of the bufferedreader class with this object as a parameter.The bufferedreader class has two constructors,Its format is:

 public bufferedreader (reader in);//Create buffer character input stream
  public bufferedreader (reader in, int size);//Create an input stream and set the buffer size

[Example] Use the bufferedreader class to read the content of a plain text file

import java.io. *;
class ep10_2 {
  public static void main (string args []) throws ioexception {
    string oneline;
    int count=0;
    try {
        filereader a=new filereader ("ep10_1.txt");
        bufferedreader b=new bufferedreader (a);
        while ((oneline=b.readline ())!=null) {//Read 1 line at a time
           count ++;//Calculate the number of rows read
           system.out.println (oneline);
        system.out.println ("\ n read a total of" + count + "lines");
        b.close ();
      catch (ioexception io) {
        system.out.println ("What went wrong!" + io);

have to be aware of is,When the read () or write () method is executed, the system may throw an ioexception exception due to an io error. You need to include the statement that performs the read and write operations in the try block and handle the possible exception through the corresponding catch block.

  • Previous Drop-down menu implemented by JS + CSS relative positioning
  • Next Summary of Winform's method for obtaining file path in C #