PUBLISHED ON: FEBRUARY 24, 2021
Java Reader Class
In this tutorial, we will learn about the Reader
class in Java. This is an abstract class and very useful to read character streams. We cannot use this class directly but subclasses of this class are very useful.
Subclasses of Reader Class
- BufferedReader
- InputStreamReader
- FileReader
- FileReader
- StringReader
Constructor
All the variants of the constructor of this class are given below.
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
Supported methods of the Reader
class is given below.
Method |
Description |
abstract void close() |
This method closes the stream and releases any system resources associated with it. |
void mark(int readAheadLimit) |
This method marks the present position in the stream. |
boolean markSupported() |
This method tells whether this stream supports the mark() operation. |
int read() |
This method reads a single character. |
int read(char[] cbuf) |
This method reads characters into an array. |
abstract int read(char[] cbuf, int off, int len) |
This method reads characters into a portion of an array. |
int read(CharBuffer target) |
This method attempts to read characters into the specified character buffer. |
boolean ready() |
This method tells whether this stream is ready to be read. |
void reset() |
This method resets the stream. |
long skip(long n) |
This method skips characters. |
Example
In the following example, we are reading data using the Reader
class. Firstly we created a reader object using FileReader class and then read the data using the read() method.
package studytonight;
import java.io.FileReader;
import java.io.Reader;
public class StudyTonight
{
public static void main(String args[])
{
try
{
Reader reader = new FileReader("E:\\studytonight\\output.txt");
int data = reader.read();
while (data != -1) {
System.out.print((char) data);
data = reader.read();
}
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Hello Studytonight
output.txt
Hello Studytonight
Read Array of Characters using Reader class
In the following example, we are using the read() method to read the data from a file, this method accepts the char array as a parameter with start offset and length. Basically, we are reading data and storing it in the character array.
package studytonight;
import java.io.FileReader;
import java.io.Reader;
public class StudyTonight
{
public static void main(String args[])
{
try
{
Reader reader = new FileReader("E:\\studytonight\\output.txt");
char[] array = new char[128];
int charsRead = reader.read(array, 0, array.length);
while(charsRead != -1) {
System.out.println(new String(array, 0, charsRead));
charsRead = reader.read(array, 0, array.length);
}
}
catch(Exception e)
{
System.out.println("Error: "+e.toString());
}
}
}
Hello Studytonight
output.txt
Hello Studytonight
Conclusion:
In this tutorial, we learned about Reader
class in Java. This class is the base class for all Reader
subclasses. This class works like an InputStream the only difference is it is character-based rather than byte-based.