The Reader class provides different methods that are implemented by its subclasses. Here are some of the commonly used methods:
- ready() - checks if the reader is ready to be read.
- read(char[] array) - reads the characters from the stream and stores in the specified array.
- read(char[] array, int start, int length) - reads the number of characters equal to length from the stream and stores in the specified array starting from the start.
- mark() - marks the position in the stream up to which data has been read.
- reset() - returns the control to the point in the stream where the mark is set.
- skip() - discards the specified number of characters from the stream.
Here is how we can implement Reader using the FileReader class. Suppose we have a file named input.txt with the following content.
Let’s try to read this file using FileReader (a subclass of Reader).
Output
In the above example, we have created a reader using the FileReader class. The reader is linked with the file input.txt.