Character streams are optimized for characters, thus providing some useful character-oriented features, and the source or target of a character stream is usually a text file. Reader and writer are the parent classes of all character streams in the java.io package. Because they are abstract classes, you should use their subclasses to create entity objects that use objects to handle related read and write operations. The subclasses of reader and writer can be divided into two categories: one to read data from a data source or to write data to a destination (called a node stream), and another to perform some sort of processing (called a process flow) on the data.
Character-oriented input stream classes are subclasses of reader, as shown in class hierarchy 10-2.
Figure 10-2 class hierarchy diagram for reader
Table 10-1 lists the main subclasses and descriptions of Reader.
Table 10-1 main subclasses of Reader
class name |
function Description |
CharArrayReader |
Input stream read from a character array |
BufferedReader |
Buffered input character stream |
Pipedreader |
Input piping |
InputStreamReader |
The input stream that converts bytes to characters |
FilterReader |
Filter input Stream |
StringReader |
Input stream read from a string |
LineNumberReader |
Append line numbers for input data |
Pushbackreader |
Returns a character and puts this byte back into the input stream |
FileReader |
Input stream read from file |
The methods provided by Reader, as shown in table 10-2, can be used to obtain bit data within the stream.
Table 10-2 Common methods of Reader
Method |
function Description |
void Close () |
Close the input stream |
void Mark () |
Mark the current position of the input stream |
Boolean marksupported () |
Test whether the input stream supports mark |
int read () |
Reads a character from the input stream |
int read (char[] ch) |
Reading an array of characters from an input stream |
int read (char[] ch, int off, int len) |
Read Len long characters from the input stream into CH |
Boolean Ready () |
Whether the test flow can read |
void Reset () |
Reposition input Stream |
Long Skip (long N) |
Skips n characters within a stream |
Reading files using the FileReader class
The FileReader class is a subclass of the reader subclass InputStreamReader class, so the FileReader class can use either the reader class's method or the InputStreamReader class's method to create the object.
When you use the FileReader class to read a file, you must first call the FileReader () construction method to create an object of the FileReader class, and then call the Read () method. The format of the FileReader constructor method is:
Public FileReader (String name); Creates a readable input stream object based on the file name
Example 10-1 uses the FileReader class to read the contents of a plain text file (view the source code).
Running result 10-3 shows:
Figure 10-3 Example 10_1 running results (output is the contents of the file Ep10_1.txt)
It is important to note that Java treats a Chinese or English letter as a character, a carriage return or a newline as two characters.
Reading files using the BufferedReader class
The BufferedReader class is used to read the data in the buffer. You must create a FileReader class object when you use it, and then create an object of the BufferedReader class with that object as a parameter. The BufferedReader class has two construction methods in the form of:
Public BufferedReader (Reader in); Creating a buffer character input stream
Public BufferedReader (Reader in,int size); Create an input stream and set the buffer size
Example 10-2 uses the BufferedReader class to read the contents of a plain text file (view the source code).
Running result 10-4 shows:
Figure 10-4 Example 10_2 running results
It is important to note that when executing the read () or write () method, the system throws a IOException exception due to an IO error, the statement that performs the read-write operation needs to be included in the try block, and the corresponding catch block is used to handle the possible exception.
Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminatesJava know how much (66) Overview of input and output (IO) and streams
Java know how much (67) character-oriented input stream