Java Reader
In Java, Reader
is an abstract class that provides a way to read characters from a stream. It is the character-based counterpart of the byte-based InputStream
class. Reader
defines a set of methods that can be used to read character data from a stream. Some commonly used methods of the Reader
class are:
read()
: This method reads a single character from the stream and returns it as an integer.read(char[] cbuf)
: This method reads characters into an array and returns the number of characters read.read(char[] cbuf, int off, int len)
: This method reads characters into a portion of an array and returns the number of characters read.skip(long n)
: This method skips over a specified number of characters in the stream.mark(int readAheadLimit)
: This method marks the current position in the stream.reset()
: This method resets the stream to the previously marked position.ready()
: This method returns true if the stream is ready to be read.close()
: This method closes the stream and releases any resources associated with it.
The Reader
class is used extensively for processing character-based data in Java. The classes that extend from this abstract class include FileReader
, StringReader
, BufferedReader
, InputStreamReader
, and others.
FileReader
is a class that reads characters from a file. StringReader
is a class that reads characters from a string. BufferedReader
is a class that reads text from a character input stream, buffering the characters to provide for the efficient reading of characters, arrays, and lines. InputStreamReader
is a class for turning a byte stream into a character stream. It is often used to convert a InputStream
to a Reader
for reading text from a binary file.