Java Reader Class

Last Updated : 2 Dec, 2026

The Reader class in Java is an abstract class in the java.io package used for reading character streams. Its subclasses provide implementations for reading text from files, arrays, strings, and other sources.

  • Implements the Readable interface to support character reading.
  • Implements the Closeable interface to support resource cleanup.

Example: Reading a text file character by character using the Reader class.

Create a file named example1.txt in the working directory with the following content:

java_reader_class
Content of example1.txt
Java
import java.io.*;
public class Geeks{
    public static void main(String[] args){
        try (Reader r = new FileReader("example1.txt")){
            int data;

            // Read characters until the end of the file
            while ((data = r.read()) != -1){
                System.out.print((char) data);
            }
        } catch (IOException e){
            System.out.println(e.getMessage());
        }
    }
}

Output:

Hello welcome to Geeks for Geeks

Explanation: The FileReader object reads characters from the file using read() until it returns -1, indicating the end of the stream. The try-with-resources statement automatically closes the reader.

Hierarchy of Reader Class

reader_

Declaration of Reader Class

public abstract class Reader implements Readable, Closeable

Since Reader is an abstract class, it cannot be instantiated directly. Its subclasses, such as FileReader and BufferedReader, are used to create reader objects.

Constructors of Reader Class

The Reader class provides two protected constructors.

  • Reader() creates a reader that uses itself as the synchronization lock.
  • Reader(Object lock) creates a reader that uses the specified object as the synchronization lock.

Methods of Reader Class

Method

Description

close()

Closes the stream and releases associated resources.

read()

Reads a single character or returns -1 at the end of the stream.

read(char[] cbuf)

Reads characters into an array.

read(char[] cbuf, int off, int len)

Reads up to len characters into an array starting at offset off.

read(CharBuffer target)

Reads characters into a CharBuffer.

mark(int readAheadLimit)

Marks the current stream position, if supported.

markSupported()

Checks whether marking is supported.

reset()

Resets the stream to a previous position, if supported.

skip(long n)

Skips characters and returns the actual number skipped.

ready()

Checks whether the stream is ready for reading without blocking.

transferTo(Writer out)

Transfers remaining characters to a Writer.

nullReader()

Returns a reader containing no characters.

of(CharSequence cs)

Creates a reader from a character sequence.

readAllAsString()

Returns the remaining content as a String.

readAllLines()

Returns the remaining lines as an unmodifiable list.

Example of Reader Class Methods

The following program uses StringReader to demonstrate common Reader methods without requiring an external file.

Java
import java.io.*;
public class Geeks{
    public static void main(String[] args) throws IOException{
        try (Reader r = new StringReader("Hello Geeks")){

            // Check whether the stream is ready
            System.out.println("Ready: " + r.ready());

            // Mark the current position
            r.mark(20);

            // Read a single character
            System.out.println("Character: " + (char) r.read());

            // Return to the marked position
            r.reset();

            // Read characters into an array
            char[] buffer = new char[5];
            int count = r.read(buffer);

            System.out.println("Buffer: " + new String(buffer, 0, count));

            // Skip the space
            r.skip(1);

            // Read the remaining characters
            while ((count = r.read()) != -1){
                System.out.print((char) count);
            }
        }
    }
}

Output
Ready: true
Character: H
Buffer: Hello
Geeks

Explanation:

  • ready() checks whether the reader is ready for reading.
  • mark() saves the current position, and reset() returns to that position.
  • read() reads individual characters or stores multiple characters in an array.
  • skip(1) skips the space between Hello and Geeks.

Subclasses of Reader Class

The Reader class provides different subclasses for reading characters from various sources.

Class

Description

FileReader

Reads characters from files.

BufferedReader

Buffers characters for efficient reading and supports reading entire lines.

CharArrayReader

Reads characters from a character array.

FilterReader

Provides a base class for filtering character streams.

InputStreamReader

Decodes byte streams into character streams.

PipedReader

Reads characters from a connected PipedWriter.

StringReader

Reads characters from a String.

Comment