🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Reader Classes  


Introduction
  • Reader is an abstract class used to read character-oriented data (16-bit Unicode) from input sources such as files, memory, or network connections.
  • It is present in the java.io package.
  • It provides basic methods like read() to read single characters, arrays of characters or lines of text from input sources.
  • Unlike InputStream which deals with bytes, Reader is specifically designed to handle characters, making it suitable for text data.
  • Below is the diagram for Reader class hierarchy: Reader class Hierarchy
  • Each of these subclasses are designed for specific purposes, which are explained below one by one.
1. BufferedReader
  • Description: Reads text from a character input stream efficiently by buffering characters, thus reducing the number of I/O operations.
  • Use in Projects: Commonly used to read text files line by line.
  • Example Syntax:
    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    String line = br.readLine();
    br.close();
2. LineNumberReader
  • Description: A subclass of BufferedReader that keeps track of line numbers while reading characters.
  • Use in Projects: Useful when you need to know the line number while reading files (like compilers or log readers).
  • Example Syntax:
    LineNumberReader lnr = new LineNumberReader(new FileReader("file.txt"));
    String line = lnr.readLine();
    int lineNumber = lnr.getLineNumber();
    lnr.close();
3. CharArrayReader
  • Description: Allows reading characters from a character array as a stream.
  • Use in Projects: Useful for testing or when data is already available in a character array.
  • Example Syntax:
    char[] data = {'J','a','v','a'};
    CharArrayReader car = new CharArrayReader(data);
    int ch = car.read();
    car.close();
4. FilterReader
  • Description: An abstract class for filtering character streams, usually extended to add custom functionalities.
  • Use in Projects: Rarely used directly; helpful when you need to create a custom filter (e.g., to modify characters while reading).
  • Example Syntax:
    // FilterReader is abstract, must be extended
    // Example: PushbackReader is one implementation
    
5. PushbackReader
  • Description: A subclass of FilterReader that allows characters to be pushed back into the stream.
  • Use in Projects: Useful in parsers when you need to “unread” characters for reprocessing.
  • Example Syntax:
    PushbackReader pr = new PushbackReader(new FileReader("file.txt"));
    int ch = pr.read();
    pr.unread(ch); // pushes back the character
    pr.close();
6. InputStreamReader
  • Description: A bridge between byte streams and character streams. Reads bytes and decodes them into characters using a specified charset.
  • Use in Projects: Commonly used with System.in for reading user input as characters.
  • Example Syntax:
    InputStreamReader isr = new InputStreamReader(System.in);
    int ch = isr.read();
    isr.close();
7. FileReader
  • Description: A convenient subclass of InputStreamReader for reading character files.
  • Use in Projects: Simplifies reading character files without manually specifying encoding (uses platform default).
  • Example Syntax:
    FileReader fr = new FileReader("file.txt");
    int ch = fr.read();
    fr.close();
8. PipedReader
  • Description: Reads characters from a connected PipedWriter, used for communication between threads.
  • Use in Projects: Useful in multithreaded applications for inter-thread communication.
  • Example Syntax:
    PipedWriter pw = new PipedWriter();
    PipedReader pr = new PipedReader(pw);
    // one thread writes to pw, another reads from pr
    
9. StringReader
  • Description: Reads characters from a string as a stream.
  • Use in Projects: Useful when working with text data already available as a string.
  • Example Syntax:
    StringReader sr = new StringReader("Hello Java");
    int ch = sr.read();
    sr.close();