DEV Community

Raj
Raj

Posted on

What is a Reader in Java?

What is a Reader in Java?
In Java a Reader is an abstract class used to read character-based data from different sources such as files text streams and input sources.

It is mainly used when we want to read text data character by character or in groups of characters.

For example if we have a text file containing names or sentences we can use Java Reader classes to read that information.
**
Types of Reader in Java**

Java provides different Reader classes for reading character data.

  1. FileReader: Used to read character data from a file.
  2. BufferedReader: Used to read text efficiently by buffering characters. It can also read a complete line using readLine().
  3. InputStreamReader: Converts byte-based input into character-based input.
  4. CharArrayReader: Used to read characters from a character array.
  5. StringReader: Used to read character data from a string.
import java.io.FileReader;

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

        FileReader reader = new FileReader("test.txt");

        int data;

        while ((data = reader.read()) != -1) {
            System.out.print((char) data);
        }

        reader.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  • FileReader opens the text file.
  • read() reads one character at a time.
  • -1 indicates that the end of the file has been reached.
  • (char) data converts the integer into a character.
  • close() closes the reader after reading.

In Java Reader is an abstract class available in the java.io package. It is used to read character-based data from different sources.

Java provides different types of Reader classes because we may need to read text from files, strings, character arrays, or byte-based input sources.

Let's understand each Reader class with examples.
1. FileReader
FileReader is used to read character data directly from a text file.

For example imagine we have a file called test.txt containing:

Hello Java
Welcome to Programming
Enter fullscreen mode Exit fullscreen mode

2. BufferedReader
BufferedReader is used to read text more efficiently by temporarily storing characters in a buffer (memory area).

Instead of reading each character directly from the source, it reads a larger block of characters into memory and then provides the data when needed.

It also provides a useful method called readLine(), which allows us to read an entire line at once.
import java.io.BufferedReader;
import java.io.FileReader;

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

        FileReader file = new FileReader("test.txt");

        BufferedReader reader = new BufferedReader(file);

        String line;

        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        reader.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  • FileReader opens the file.
  • BufferedReader wraps the FileReader to provide buffered reading.
  • readLine() reads one complete line at a time.
  • The loop continues until readLine() returns null, meaning the end of the file.
  • close() closes the reader and its underlying file reader.

3. InputStreamReader
InputStreamReader is used to convert byte-based input into character-based input.

In Java, some input sources provide data in bytes rather than characters. For example, keyboard input and network streams can provide byte-based input.

InputStreamReader acts as a bridge between byte streams and character streams. It converts bytes into characters using a character encoding.


import java.io.InputStreamReader;

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

        InputStreamReader reader =
            new InputStreamReader(System.in);

        System.out.print("Enter your name: ");

        int data;

        while ((data = reader.read()) != '\n' && data != -1) {
            System.out.print((char) data);
        }

        reader.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  • System.in receives input from the keyboard as bytes.
  • InputStreamReader converts those bytes into characters.
  • reader.read() reads one character at a time.
  • The loop stops when the user presses Enter (or the input ends).

4. CharArrayReader
CharArrayReader is used to read character data from an existing character array.

A character array is a collection of characters stored in an array. Instead of reading data from a file, we can directly read characters from an array in memory.

import java.io.CharArrayReader;

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

        char[] data = {'H', 'e', 'l', 'l', 'o'};

        CharArrayReader reader = new CharArrayReader(data);

        int value;

        while ((value = reader.read()) != -1) {
            System.out.print((char) value);
        }

        reader.close();
    }
}

Enter fullscreen mode Exit fullscreen mode
output:

Hello
Enter fullscreen mode Exit fullscreen mode

How it works:

  • First, we create a character array containing the word Hello.
  • CharArrayReader takes that character array as input.
  • read() reads one character at a time.
  • The loop continues until all characters are read.
  • close() closes the reader.
  • StringReader StringReader is used to read character data directly from a String.

For example, if we already have a sentence stored in a String variable, we can use StringReader to read its characters one by one.
import java.io.StringReader;

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

        String text = "Hello Java";

        StringReader reader = new StringReader(text);

        int data;

        while ((data = reader.read()) != -1) {
            System.out.print((char) data);
        }

        reader.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  • First we store the text inside a String variable.
  • StringReader takes that String as input.
  • read() reads one character at a time.
  • The loop continues until all characters are read.
  • close() closes the reader.

Top comments (0)