The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding
read request to be made of the underlying character or byte stream. It is
therefore advisable to wrap a BufferedReader around any
Reader whose read() operations may be costly, such as
FileReaders and InputStreamReaders. For
example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
read() or readLine() could cause bytes to be
read from the file, converted into characters, and then returned, which can
be very inefficient.
Programs that use DataInputStreams for textual input can be
localized by replacing each DataInputStream with an appropriate
BufferedReader.
API Note
Once wrapped in a BufferedReader, the underlying
Reader should not be used directly nor wrapped with
another reader.
FileReader, InputStreamReader, java.nio.file.Files#newBufferedReader
| Modifier and Type | Field and Description |
|---|---|
| private char[] | |
| private static final int | |
| private static final int | |
| private Reader | |
| private static final int | |
| private int | |
| private boolean | markedSkipLF
The skipLF flag when the mark was set |
| private int | |
| private int | |
| private int | |
| private boolean | skipLF
If the next character is a line feed, skip it |
| private static final int |
| Access | Constructor and Description |
|---|---|
| public | BufferedReader(Reader
A Reader in, int Input-buffer size sz)Creates a buffering character-input stream that uses an input buffer of the specified size. |
| public | BufferedReader(Reader
A Reader in)Creates a buffering character-input stream that uses a default-sized input buffer. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements abstract java. Implements java. Closes the stream and releases any system resources associated with it. |
| private void | |
| private void | |
| public Stream | Returns: aStream<String> providing the lines of text
described by this BufferedReaderReturns a |
| public void | mark(int
Limit on the number of characters that may be
read while still preserving the mark. An attempt
to reset the stream after reading characters
up to this limit or beyond may fail.
A limit value larger than the size of the input
buffer will cause a new buffer to be allocated
whose size is no smaller than limit.
Therefore large values should be used with care. readAheadLimit)Overrides java. Marks the present position in the stream. |
| public boolean | markSupported()
Overrides java. Tells whether this stream supports the mark() operation, which it does. |
| public int | 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 reachedOverrides java. Reads a single character. |
| public int | read(char[] cbuf, int off, int len)
Implements abstract java. Reads characters into a portion of an array. |
| private int | read1(char[] cbuf, int off, int len)
Reads characters into a portion of an array, reading from the underlying stream if necessary. |
| pack-priv String | Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached without reading any charactersIf true, the next '\n' will be skipped ignoreLF, boolean[] Output: Whether a line terminator was encountered
while reading the line; may be term)null.Reads a line of text. |
| public String | Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached without reading any charactersReads a line of text. |
| public boolean | |
| public void | |
| public long |
| cb | back to summary |
|---|---|
| private char[] cb | |
| DEFAULT_CHAR_BUFFER_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_CHAR_BUFFER_SIZE | |
| DEFAULT_EXPECTED_LINE_LENGTH | back to summary |
|---|---|
| private static final int DEFAULT_EXPECTED_LINE_LENGTH | |
| in | back to summary |
|---|---|
| private Reader in | |
| INVALIDATED | back to summary |
|---|---|
| private static final int INVALIDATED | |
| markedChar | back to summary |
|---|---|
| private int markedChar | |
| markedSkipLF | back to summary |
|---|---|
| private boolean markedSkipLF The skipLF flag when the mark was set | |
| nChars | back to summary |
|---|---|
| private int nChars | |
| nextChar | back to summary |
|---|---|
| private int nextChar | |
| readAheadLimit | back to summary |
|---|---|
| private int readAheadLimit | |
| skipLF | back to summary |
|---|---|
| private boolean skipLF If the next character is a line feed, skip it | |
| UNMARKED | back to summary |
|---|---|
| private static final int UNMARKED | |
| BufferedReader | back to summary |
|---|---|
| public BufferedReader(Reader in, int sz) Creates a buffering character-input stream that uses an input buffer of the specified size.
| |
| BufferedReader | back to summary |
|---|---|
| public BufferedReader(Reader in) Creates a buffering character-input stream that uses a default-sized input buffer.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements abstract java. Implements java. Doc from java. 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.
| |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() throws IOException Checks to make sure that the stream has not been closed | |
| fill | back to summary |
|---|---|
| private void fill() throws IOException Fills the input buffer, taking the mark into account if it is valid. | |
| lines | back to summary |
|---|---|
| public Stream Returns a The reader must not be operated on during the execution of the terminal stream operation. Otherwise, the result of the terminal stream operation is undefined. After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line. If an | |
| mark | back to summary |
|---|---|
| public void mark(int readAheadLimit) throws IOException Overrides java. Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
| |
| markSupported | back to summary |
|---|---|
| public boolean markSupported() Overrides java. Tells whether this stream supports the mark() operation, which it does. | |
| read | back to summary |
|---|---|
| public int read() throws IOException Overrides java. Reads a single character.
| |
| read | back to summary |
|---|---|
| public int read(char[] cbuf, int off, int len) throws IOException Implements abstract java. Reads characters into a portion of an array. This method implements the general contract of the corresponding
read on the underlying stream returns
-1 to indicate end-of-file then this method returns
-1. Otherwise this method returns the number of characters
actually read.
Subclasses of this class are encouraged, but not required, to attempt to read as many characters as possible in the same fashion. Ordinarily this method takes characters from this stream's character
buffer, filling it from the underlying stream as necessary. If,
however, the buffer is empty, the mark is not valid, and the requested
length is at least as large as the buffer, then this method will read
characters directly from the underlying stream into the given array.
Thus redundant
| |
| read1 | back to summary |
|---|---|
| private int read1(char[] cbuf, int off, int len) throws IOException Reads characters into a portion of an array, reading from the underlying stream if necessary. | |
| readLine | back to summary |
|---|---|
| pack-priv String readLine(boolean ignoreLF, boolean[] term) throws IOException Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), a carriage return followed immediately by a line feed, or by reaching the end-of-file (EOF).
| |
| readLine | back to summary |
|---|---|
| public String readLine() throws IOException Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), a carriage return followed immediately by a line feed, or by reaching the end-of-file (EOF).
| |
| ready | back to summary |
|---|---|
| public boolean ready() throws IOException Overrides java. Tells whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready.
| |
| reset | back to summary |
|---|---|
| public void reset() throws IOException Overrides java. Resets the stream to the most recent mark.
| |
| skip | back to summary |
|---|---|
| public long skip(long n) throws IOException Overrides java. Doc from java. Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. If the stream is already at its end before this method is invoked, then no characters are skipped and zero is returned.
| |