Java Reader Class

The Java Reader class serves as an abstraction for reading character streams. Subclasses are required to implement the read(char, int, int) and close methods. Typically, subclasses will customize these methods to enhance performance, add extra features, or both.

Several examples of implementation classes include BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, and StringReader.

Fields

Modifier and Type Field Description
protected Object lock The object used to synchronize operations on this stream.

Constructor

Modifier Constructor Description
protected Reader() It creates a new character-stream reader whose critical sections will synchronize on the reader itself.
protected Reader(Object lock) It creates a new character-stream reader whose critical sections will synchronize on the given object.

Methods

Modifier and Type Method Description
abstract void close() It closes the stream and releases any system resources associated with it.
void mark(int readAheadLimit) It marks the present position in the stream.
boolean markSupported() It tells whether this stream supports the mark() operation.
int read() It reads a single character.
int read(char[] cbuf) It reads characters into anarray.
abstract int read(char[] cbuf, int off, int len) It reads characters into a portion of an array.
int read(CharBuffer target) It attempts to read characters into the specified character buffer.
boolean ready() It tells whether this stream is ready to be read.
void reset() It resets the stream.
long skip(long n) It skips characters.

Example

Example

import java.io.*;

public class ReaderExample {

	public static void main(String[] args) {

		try {

			Reader reader = new FileReader("file.txt");

			int data = reader.read();

			while (data != -1) {

				System.out.print((char) data);

				data = reader.read();

			}

            reader.close();

		} catch (Exception ex) {

			System.out.println(ex.getMessage());

		}

	}

}

file.txt:

Example

I love my country

Output:

Output

I love my country

Input Required

This code uses input(). Please provide values below: