Java Reader class is an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
Constructors in Java Reader Class
There are two constructors used with Java Reader Class as mentioned below:
- protected Reader(): Creates a new character-stream reader whose critical sections will synchronize on the reader itself.
- protected Reader(Object lock): Creates a new character-stream reader whose critical sections will synchronize on the given object.
Methods in Java Reader Class
1. abstract void close()
Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
Syntax: public abstract void close() throws IOException
Throws:
IOException
2. void mark(int readAheadLimit)
Marks the present position in the stream.Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.
Syntax: public void mark(int readAheadLimit) throws IOException
Parameters:
readAheadLimit - Limit on the number of characters that may be read
while still preserving the mark. After reading this many characters,
attempting to reset the stream may fail.
Throws:
IOException
3. boolean markSupported()
Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.
Syntax :public boolean markSupported()
Returns:
true if and only if this stream supports the mark operation.
4. int read()
Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method.
Syntax :public int read()
throws IOException
Returns:
The character read, as an integer in the range 0 to 65535 (0x00-0xffff),
or -1 if the end of the stream has been reached
Throws:
IOException
5. int read(char[] cbuf)
Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Syntax :public int read(char[] cbuf)
throws IOException
Parameters:
cbuf - Destination buffer
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException
6. abstract int read(char[] cbuf, int off, int len)
Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Syntax :public abstract int read(char[] cbuf,int off,int len) throws IOException
Parameters:
cbuf - Destination buffer
off - Offset at which to start storing characters
len - Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException
7. int read(CharBuffer target)
Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.
Syntax :public int read(CharBuffer target) throws IOException
Parameters:
target - the buffer to read characters into
Returns:
The number of characters added to the buffer,
or -1 if this source of characters is at its end
Throws:
IOException
NullPointerException
ReadOnlyBufferException
8. boolean ready()
Tells whether this stream is ready to be read.
Syntax :public boolean ready() throws IOException
Returns:
True if the next read() is guaranteed not to block for input, false otherwise.
Note that returning false does not guarantee that the next read will block.
Throws:
IOException
9. void reset()
Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().
Syntax :public void reset() throws IOException
Throws:
IOException
10. long skip(long n)
Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.
Syntax :public long skip(long n) throws IOException
Parameters:
n - The number of characters to skip
Returns:
The number of characters actually skipped
Throws:
IllegalArgumentException - If n is negative.
IOException
Example
Java
import java.io.*;
import java.nio.CharBuffer;
import java.util.Arrays;
class ReaderDemo {
public static void main(String[] args)
throws IOException
{
Reader r = new FileReader(" file.txt & quot;);
PrintStream out = System.out;
char c[] = new char [ 10 ];
CharBuffer cf = CharBuffer.wrap(c);
if (r.markSupported()) {
r.mark( 100 );
out.println("
mark method is supported & quot;);
}
r.skip( 5 );
if (r.ready()) {
r.read(c, 0 , 10 );
out.println(Arrays.toString(c));
r.read(cf);
out.println(Arrays.toString(cf.array()));
out.println(( char )r.read());
}
r.close();
}
}
|
Output
[f, g, h, i, g, k, l, m, n, o]
[p, q, r, s, t, u, v, w, x, y]
z
Implementation of Reader Classes
Some of the implementations of Reader classes in Java are mentioned below:
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
12 Sep, 2023
Like Article
Save Article