Table of contents
1.
Introduction
2.
Reader Class
3.
Methods of Reader Class
3.1.
ready()
3.2.
read() 
3.3.
read(char[] array)
3.4.
read(char[] array, int start_index, int total)
3.5.
skip(long n)
3.6.
markSupported()
3.7.
mark(int readAheadLimit)
3.8.
reset()
3.9.
close()
4.
Example
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024

Reader Class in Java

Author Pradeep Kumar
1 upvote

Introduction

In this blog, we will look into the Reader class of Java. Reader class allows you to read characters from a stream. We will discuss the different methods that should be overridden by the subclasses of the Reader class. 

If you are new to Java and are yet to write your first program in the language, you can check out this article to get started with the language.

Must Read, Multithreading in java, Duck Number in Java

Reader Class

The Reader is an abstract superclass for reading character streams in the java.io package. We can't use it directly because it's an abstract class.  But its subclasses can be used to read streams.

To use the Reader class in java, we need to import the java.io.Reader package. Here's how we can create a reader:

Reader r = new FileReader(<file_name>);

In this case, we have created a Reader using the FileReader class. We can create readers from other subclasses of Reader also.

Also see,  Swap Function in Java

Methods of Reader Class

Now that we have discussed what a Reader class is and how to instantiate one from FileReader class or any other subclasses of the Reader class. Let's discuss the most common methods used by the Reader class:

ready()

This method returns a boolean value that indicates whether or not the stream is ready to read.

read() 

This method only reads one character. It returns an integer value, which is the integer value of the character read (i.e., between 0 and 65535) or -1, in case the end of the stream has reached, and there was no character to be read. 

read(char[] array)

It reads the characters from the stream and stores them in the given array. This method returns an output of the Integer type. It returns the number of characters read by the method, or -1, in case the end of the stream has reached, and there are no more characters to be read.

read(char[] array, int start_index, int total)

It reads characters from the stream and stores them in the given array. Here, start_index is the index from where it starts storing the characters. The total variable gives the maximum number of characters that are to be read from the input stream.

skip(long n)

To skip some characters, you can call this function. It returns a value of long type stating the number of characters skipped, which will be less than or equal to n. The output will be less than n if the end of the stream is reached before it could skip n characters.  

markSupported()

This method returns a boolean value indicating whether or not the stream supports the mark() operation.

mark(int readAheadLimit)

It marks the current position in the stream. So, next time, when you call the reset() function, the stream will be reset to this point. Here, readhAheadLimit is the limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.

reset()

It resets the stream. In the case of a marked stream, it will reset the stream to the mark. But if the stream is not marked, it will reset it to the starting position.

close()

Upon invoking this method, the stream will be closed, and it will free all the system resources associated with it.

Also see, Hashcode Method in Java

Example

Let's understand the above-stated methods with the help of an example. In this example, we will use the FileReader class to read characters from a file. The file we are using here to be read by the program is input.txt. Contents of the file are:

input.txt

Welcome to Coding Ninjas! Through this example file, we will learn the functionality of a Reader Class.

In this program, we test different methods of a java reader class. 

Test.java

import java.io.*;
class Test
{
    public static void main(String[] args) {
        try{
            // Instantiating the reader to read the input file.
            Reader input_stream = new FileReader("input.txt");
           
            //check if the stream is ready?
            System.out.println("Is the stream ready? " + input_stream.ready());
           
            // Reading the first character
            char first_char = (char) input_stream.read();
            // Printing the first character
            System.out.println("The first character is: " + first_char);
           
            // skip the next seven character
            input_stream.skip(7);

            // check if the stream supports the mark operation
            if(input_stream.markSupported()){
                // mark the stream
                System.out.println("Input stream supports the mark method");
                input_stream.mark(200);
            }
            else{
                System.out.println("Mark method not supported");
            }
           
            // Reading the characters in the array
            char[] array = new char [200]; // Instantialising the array
            input_stream.read(array);
            System.out.println("Remaining data captured in the array: ");
            System.out.println(array);
           
            input_stream.close(); // closing the reader

        }
        catch(Exception e) {
            // Print the error in case any exception occurs
            System.out.println("Error: An error has been occured");
            System.out.println(e);
            e.getStackTrace();
        }
    }
}

Here, Firstly, we have checked whether the stream is ready to be read. Then we have printed the first character of the file. Then we skipped the next seven characters. We then checked it the stream supports the mark method or not. Then we have printed the remaining content of the stream.

Output:

Is the stream ready? true
The first character is: W
Mark method not supported
Remaining data captured in the array:
to Coding Ninjas! Through this example file, we will learn the functionality of a Reader Class.


As you can see from the output, the stream has "W" as the first character, and it doesn't support the mark method.

Try it on online java compiler.

Frequently Asked Questions

1. What is the default value of the method markSupported()?
Ans: The default implementation of this method always returns false. Subclasses of the reader class can override this method to change the returned value upon execution of the function.

2. Does the FileReader class supports the mark operation?
Ans: No, the FileReader class doesn't support the mark operation. It doesn't have an overridden method for this. Hence, it will always return false.

3. List some subclasses of the Reader class.
Ans: BufferedReader, FileReader, InputStreamReader, and StringReader are some of the most commonly used subclasses of the Reader class.

Conclusion

In this article, we have extensively discussed the Reader class of Java. We discussed different methods supported by the Reader class. We hope that this blog has helped you enhance your knowledge regarding the Reader class, and if you like to learn more, check out our article on Writer Class. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass