Closeable, AutoCloseable, ReadableBufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReaderpublic abstract class Reader extends Object implements Readable, Closeable
| Modifier and Type | Field | Description |
|---|---|---|
protected Object |
lock |
The object used to synchronize operations on this stream. |
| Modifier | Constructor | Description |
|---|---|---|
protected |
Creates a new character-stream reader whose critical sections will synchronize on the reader itself. |
|
protected |
Creates a new character-stream reader whose critical sections will synchronize on the given object. |
| Modifier and Type | Method | Description |
|---|---|---|
abstract void |
close() |
Closes the stream and releases any system resources associated with it. |
void |
mark |
Marks the present position in the stream. |
boolean |
markSupported() |
Tells whether this stream supports the mark() operation. |
static Reader |
nullReader() |
Returns a new Reader that reads no characters. |
static Reader |
of |
Returns a Reader that reads characters from a CharSequence. |
int |
read() |
Reads a single character. |
int |
read |
Reads characters into an array. |
abstract int |
read |
Reads characters into a portion of an array. |
int |
read |
Attempts to read characters into the specified character buffer. |
String |
readAllAsString() |
Reads all remaining characters into a string. |
List |
readAllLines() |
Reads all remaining characters as lines of text. |
boolean |
ready() |
Tells whether this stream is ready to be read. |
void |
reset() |
Resets the stream. |
long |
skip |
Skips characters. |
long |
transferTo |
Reads all characters from this reader and writes the characters to the given writer in the order that they are read. |
protected Object lock
this or a synchronized method.protected Reader()
protected Reader(Object lock)
lock - The Object to synchronize on.public static Reader nullReader()
Reader that reads no characters. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect. While the stream is open, the read(), read(char[]), read(char[], int, int), read(CharBuffer),
ready(), skip(long), and transferTo() methods all behave as if end of stream has been reached. After the stream has been closed, these methods all throw IOException.
The markSupported() method returns false. The mark() and reset() methods throw an IOException.
The object used to synchronize operations on the returned Reader is not specified.
Reader which reads no characterspublic static Reader of(CharSequence cs)
Reader that reads characters from a CharSequence. The reader is initially open and reading starts at the first character in the sequence. The returned reader supports the mark() and reset() operations.
The resulting reader is not safe for use by multiple concurrent threads. If the reader is to be used by more than one thread it should be controlled by appropriate synchronization.
If the sequence changes while the reader is open, e.g. the length changes, the behavior is undefined.
cs - CharSequence providing the character stream.Reader which reads characters from cs
NullPointerException - if cs is null
public int read(CharBuffer target) throws IOException
read in interface Readable
target - the buffer to read characters intoIOException - if an I/O error occursNullPointerException - if target is nullReadOnlyBufferException - if target is a read only buffer, even if its length is zeropublic int read() throws IOException
Subclasses that intend to support efficient single-character input should override this method.
0x00-0xffff), or -1 if the end of the stream has been reachedIOException - If an I/O error occurspublic int read(char[] cbuf) throws IOException
If the length of cbuf is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.
cbuf - Destination bufferIOException - If an I/O error occurspublic abstract int read(char[] cbuf, int off, int len) throws IOException
If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.
cbuf - Destination bufferoff - Offset at which to start storing characterslen - Maximum number of characters to readIndexOutOfBoundsException - If off is negative, or len is negative, or len is greater than cbuf.length - off
IOException - If an I/O error occurspublic List<String> readAllLines() throws IOException
When this reader reaches the end of the stream, further invocations of this method will return an empty list.
A line is either a sequence of zero or more characters followed by a line terminator, or it is a sequence of one or more characters followed by the end of the stream. A line does not include the line terminator.
A line terminator is one of the following: a line feed character "\n" (U+000A), a carriage return character "\r" (U+000D), or a carriage return followed immediately by a line feed "\r\n" (U+000D U+000A).
The behavior for the case where the reader is asynchronously closed, or the thread interrupted during the read, is highly reader specific, and therefore not specified.
If an I/O error occurs reading from the stream then it may do so after some, but not all, characters have been read. Consequently the stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the reader be promptly closed if an I/O error occurs.
List of Strings in the order they are readIOException - If an I/O error occursOutOfMemoryError - If the number of remaining characters exceeds the implementation limit for String.public String readAllAsString() throws IOException
When this reader reaches the end of the stream, further invocations of this method will return an empty string.
The behavior for the case where the reader is asynchronously closed, or the thread interrupted during the read, is highly reader specific, and therefore not specified.
If an I/O error occurs reading from the stream then it may do so after some, but not all, characters have been read. Consequently the stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the reader be promptly closed if an I/O error occurs.
String. It is not suitable for reading input from an unknown origin, as this may result in the allocation of an arbitrary amount of memory.String containing all remaining charactersIOException - If an I/O error occursOutOfMemoryError - If the number of remaining characters exceeds the implementation limit for String.public long skip(long n) throws IOException
n - The number of characters to skipIllegalArgumentException - If n is negative.IOException - If an I/O error occurspublic boolean ready() throws IOException
IOException - If an I/O error occurspublic boolean markSupported()
public void mark(int readAheadLimit) throws IOException
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.IOException - If the stream does not support mark(), or if some other I/O error occurspublic void reset() throws IOException
IOException - If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurspublic abstract void close() throws IOException
close in interface AutoCloseable
close in interface Closeable
IOException - If an I/O error occurspublic long transferTo(Writer out) throws IOException
This method may block indefinitely reading from the reader, or writing to the writer. The behavior for the case where the reader and/or writer is asynchronously closed, or the thread interrupted during the transfer, is highly reader and writer specific, and therefore not specified.
If the total number of characters transferred is greater than Long.MAX_VALUE, then Long.MAX_VALUE will be returned.
If an I/O error occurs reading from the reader or writing to the writer, then it may do so after some characters have been read or written. Consequently the reader may not be at end of the stream and one, or both, streams may be in an inconsistent state. It is strongly recommended that both streams be promptly closed if an I/O error occurs.
out - the writer, non-nullIOException - if an I/O error occurs when reading or writingNullPointerException - if out is null
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/io/Reader.html