Reader Class in Java

The reader class in Java is an abstract class defined in package and superclass for all character input streams. The reader class and all its subclasses were introduced in Java 1.1. It implements Readable, and Closeable marker interfaces. Reader interface defines read() method and Closable interface defines close() method.

public abstract class Reader
extends Object
implements Readable, Closeable

All subclass of Reader are used to read the character/text data from the file or keyboard or network.

Java Reader Class Methods

In Reader class there are different overloaded read() methods are given,

Modifier and TypeMethodDesription
intread()It reads a single character at a time.
intread(char[] ch)It reads characters, stores them to the passed char array, and returns the number of characters it read.
abstract intread(char[] ch, int off, int len)It reads characters from offset to len, store them to the passed char array, and returns the number of characters it read.
intread(CharBuffer target)It read characters into the specified character buffer. Return number of characters added to the buffer.

All overloaded forms of the read methods will read data until some input is available, an I/O error occurs, or the end of the stream is reached. They return -1 when the end of the stream has been reached.

Modifier and TypeMethodDescritption
booleanready()Tells whether the stream is ready to be read.
longskip(long n)It skip number of given characters.
booleanmarkSupport()Tells whether this stream supports the mark() operation. Default implementaion always returns false.
voidmark(int readAheadLimit)It marks the present position in the stream.
voidreset()It reset the stream.
Abstract voidclose()It closes the stream and releases any system resources associated with it.

It also have transferTo() method, which was introduced in Java 10 version.

longtransferTo(Writer out)It reads all characters from the reader and writes the characters to the given writer in the order that they are read.

The transferTo() method doesn’t close either the Reader and Writer. It rutruns NullPointerException if the passed Writer object out is null, otherwise it ruturns number of characters transfered.

Note:- Except markSupport() all other methods of class throws IOException.

Since it is a connection based object and uses different resources of the computer. Therefore, after completion of the program, those connections should be closed and all related resources should be released. For this, close() is given and it is defined in the Closeable interface. The actual close() method is defined in the AutoCloseable marker interface which was introduced in Java 1.7 version. The Closeable interface is extended from the AutoClosable interface.

Subclasses of Reader


The LineNumberReader class is subclass of BufferedReader, PushBackReader is the sub class of FilteredReader, and FileReader is the subclass of InputStreamReader. The BufferedReader, InputStreamReader and FileReader are the most important sub classes of the Reader.

None of these subclasses having 0 parameter constructor because it is a stream-based connection where one end is Java application. Therefore second end information must be passed as input to the constructor.

Among all these subclasses only FileReader class can directly communicate with files. FileReader class has several constructors with File as parameters. Similarly, only InputStreamReader can directly communicate to the console. The remaining subclasses are dependent on the FileReader class to communicate with the file, dependent on InputStreamReader to communicate with the console.

The InputStreamReader is a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset.

Among these all subclasses BufferedReader is the most convenient way to read character or text data. It has a method to read one line at a time, which makes our tasks very easy compare to other classes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply