![]() |
VOOZH | about |
The Reader class in Java is an abstract class for reading character streams. Its subclasses (FileReader, BufferedReader) provide implementations, with read() being the main method to read characters.
Declaration of Reader class is given below:
public abstract class Reader implements Readable, Closeable
Example: Read a text file character by character using the Reader class.
Output :
Note: To ensure the program runs correctly, create a file named example1.txt in the working directory.
Add the following content to the file, or you can add any text.
Hello welcome to Geeks for Geeks
Save the file and run the program. The program will read and display the contents of example1.txt as output.
There are two constructors used with Java Reader Class as mentioned below:
| Method | Description |
|---|---|
| abstract void close() | Closes the stream and releases resources. |
| void mark(int readAheadLimit) | Marks the current position in the stream. |
| boolean markSupported() | Checks if mark/reset operations are supported. |
| int read() | Reads a single character from the stream. Returns -1 if the end is reached. |
| int read(char[] cbuf) | Reads multiple characters into an array. |
| abstract int read(char[] cbuf, int off, int len) | Reads a portion of the array starting at offset off for length len. |
| int read(CharBuffer target) | Reads characters into a CharBuffer object. |
| void reset() | Resets the stream to the most recent mark position. |
| long skip(long n) | Skips the specified number of characters. |
Example: The below program demonstrate the working of various functionalities of the Reader class in Java.
Output:
Some of the implementations of Reader classes in Java are mentioned below: