Java Reader Class

Java Reader Classes

What is Java Reader Class?

  • In Java, Reader is an abstract class that provides a standard way to read characters from a stream of data.
  • It is the base class for all character input stream classes, such as FileReader, StringReader, and InputStreamReader.
  • It is a part of the Java IO API, which provides a set of classes and interfaces for performing input and output operations.
  • To understand the Java Reader Class, Read the Complete Article.

Subclasses of Reader: 

  • Here are some of the commonly used subclasses of Reader in Java:
    • BufferedReader: A Reader subclass that efficiently reads characters from a character stream by buffering them. It offers strategies for reading entire lines of text at once as opposed to single characters.
    • InputStreamReader: A subclass of Reader that reads characters from an input stream while decoding the characters it encounters. It is frequently used to read text from a network socket or standard input.
    • CharArrayReader: A Reader subclass that reads characters from a character array that is stored in memory. It is helpful when reading characters from a String or char array without first constructing a new String object.
    • StringReader: A subclass of Reader that reads characters from a String. It is similar to CharArrayReader, but takes a String object as input instead of a char array.
    • PipedReader: A subclass of Reader that provides a way to connect two threads through a pipe. One thread writes data to a PipedWriter, while the other thread reads data from a PipedReader.

These subclasses provide additional functionality and convenience methods on top of the basic methods defined in the Reader class, making it easier to read characters from different types of sources.

Java Reader Class

Let’s look at the Java Reader Class to perform certain operations.

Example 1: Reader using ReaderFile

Run

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class Main {
    public static void main(String[] args) {
        String data = "This is a test string.";
        try (Reader reader = new StringReader(data)) {
            int character;
            while ((character = reader.read()) != -1) {
                System.out.print((char) character);
            }
        } catch (IOException e) {
            System.err.println("Error reading data: " + e.getMessage());
        }
    }
}

Output

This is a test string.

Example 2 : Reader Using FileReader

Run

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class Main
{
  public static void main (String[]args)
  {
    String data = "PrepInsta Prime is the New Netflix.";
      try (Reader reader = new StringReader (data))
    {
      int character;
      while ((character = reader.read ()) != -1)
	{
	  System.out.print ((char) character);
	}
    } catch (IOException e)
    {
      System.err.println ("Error reading data: " + e.getMessage ());
    }
  }
}

Output

PrepInsta Prime is the New Netflix.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription