Character streams are optimized for the characteristics of character data, thus providing some useful characters-oriented features, and the source or target of the character stream is usually a text file. Reader and Writer are the parent classes of all character streams in the java.io package. Since they are both abstract classes, their subclasses should be used to create entity objects and 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 flow), and the other is used to perform some processing on the data (called a node flow). Processing stream).
Character-oriented input stream classes are subclasses of Reader, and their class hierarchy is shown in the figure.
The following table lists the main subclasses and descriptions of Reader.
The methods provided by Reader are shown in this table. These methods can be used to obtain bit data in the stream:
Read files using FileReader class
The FileReader class is a subclass of the InputStreamReader class, the FileReader class can use both the Reader class methods or the InputStreamReader class methods to create objects.
When reading a file using the FileReader class, you must first call the FileReader() constructor to create an object of the FileReader class, and 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 FileReader class to read the contents of plain text files
import java.io.*;class ep10_1{ public static void main(String args[]) throws IOException{ char a[]=new char[1000]; //Create an array FileReader that can hold 1000 characters b=new FileReader( "ep10_1.txt"); int num=b.read(a); //Read the data into array a and return the number of characters String str=new String(a,0,num); //Read the string Convert an array into a string System.out.println("The number of characters read is: "+num+", the content is: /n"); System.out.println(str); }}
It should be noted that Java treats a Chinese character or English letter as one character, and carriage return or line break as two characters.
Read files using the BufferedReader class
The BufferedReader class is used to read data in a buffer. When using it, you must create a FileReader class object, and then use this object as a parameter to create an object of the BufferedReader class. The BufferedReader class has two constructors, and its format is:
public BufferedReader(Reader in); //Create a 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 contents 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 F ileReader("ep10_1.txt"); BufferedReader b=new BufferedReader(a); while((OneLine=b.readLine())!=null){ //Read 1 row count++ each time; //Calculate the number of rows read System.out.println(OneLine); } System .out.println("/n A total of "+count+" lines" were read; b.close(); } catch(IOException io){ System.out.println("Error!"+io); } } }
It should be noted that when executing the read() or write() method, the system may throw an IOException exception due to an IO error. The statements that perform read and write operations need to be included in the try block and handled through the corresponding catch block. The exception generated.