in , ,

Java Reader Class

Java Reader Class
Java Reader Class

In this tutorial, we will learn about Java Reader, its subclasses, and its strategies with the help of an example.

The Reader class of the package is an abstract superclass that represents a stream of characters.

Since Reader is an abstract class, it isn’t useful without anyone else. In any case, its subclasses can be used to understand data.

Subclasses of Reader

In order to use the functionality of Reader, we can use its subclasses. Some of them are:

We will learn about all these subclasses in the next tutorial.

Make a Reader

To make a Reader, we should import the package first. When we import the package, here is the means by which we can make the reader.

// Creates a Reader
Reader input = new FileReader();

Here, we have made a reader using the FileReader class. It is on the grounds that Reader is an abstract class. Subsequently we can’t make an object of Reader.

Note: We can likewise make readers from different subclasses of Reader.

Strategies for Reader

The Reader class gives various techniques that are executed by its subclasses. Here is a portion of the generally used techniques:

  • 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

Example: Reader Using FileReader

Here is how we can implement Reader using the FileReader class.

Suppose we have a file named input.txt with the following content.

This is a line of text inside the file.

Let’s try to read this file using FileReader (a subclass of Reader).


class Main {
    public static void main(String[] args) {

        // Creates an array of character
        char[] array = new char[100];

        try {
            // Creates a reader using the FileReader
            Reader input = new FileReader("input.txt");

            // Checks if reader is ready 
            System.out.println("Is there data in the stream?  " + input.ready());

            // Reads characters
            System.out.println("Data in the stream:");

            // Closes the reader

        catch(Exception e) {


Is there data in the stream?  true
Data in the stream:
This is a line of text inside the file.

In the above example, we have made a reader using the FileReader class. The reader is connected with the record input.txt.

Reader input = new FileReader("input.txt");

To read data from the input.txt file, we have implemented these methods.;       // to read data from the reader
input.close();      // to close the reader

To learn more, visit Java Reader (official Java documentation).

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java PrintStream Class

Java PrintStream Class

Java Writer Class

Java Writer Class