public class BufferedReader extends Reader
Reader and buffers the input. Expensive
interaction with the underlying reader is minimized, since most (smaller)
requests can be satisfied by accessing the buffer alone. The drawback is that
some extra space is required to hold the buffer and that copying takes place
when filling that buffer, but this is usually outweighed by the performance
benefits.
A typical application pattern for the class looks like this:
BufferedReader buf = new BufferedReader(new FileReader("file.java"));
BufferedWriter| Constructor and Description |
|---|
BufferedReader(Reader in)
Constructs a new
BufferedReader, providing in with a buffer
of 8192 characters. |
BufferedReader(Reader in,
int size)
Constructs a new
BufferedReader, providing in with size characters
of buffer. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this reader.
|
Stream<String> |
lines()
Returns a
Stream, the elements of which are lines read from
this BufferedReader. |
void |
mark(int markLimit)
Sets a mark position in this reader.
|
boolean |
markSupported()
Indicates whether this reader supports the
mark() and
reset() methods. |
int |
read()
Reads a single character from this reader and returns it with the two
higher-order bytes set to 0.
|
int |
read(char[] buffer,
int offset,
int length)
Reads up to
length characters from this reader and stores them
at offset in the character array buffer. |
String |
readLine()
Returns the next line of text available from this reader.
|
boolean |
ready()
Indicates whether this reader is ready to be read without blocking.
|
void |
reset()
Resets this reader's position to the last
mark() location. |
long |
skip(long charCount)
Skips at most
charCount chars in this stream. |
nullReader, read, read, transferTopublic BufferedReader(Reader in)
BufferedReader, providing in with a buffer
of 8192 characters.in - the Reader the buffer reads from.public BufferedReader(Reader in, int size)
BufferedReader, providing in with size characters
of buffer.in - the InputStream the buffer reads from.size - the size of buffer in characters.IllegalArgumentException - if size <= 0.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class ReaderIOException - if an error occurs while closing this reader.public void mark(int markLimit)
throws IOException
markLimit
indicates how many characters can be read before the mark is invalidated.
Calling reset() will reposition the reader back to the marked
position if markLimit has not been surpassed.mark in class ReadermarkLimit - the number of characters that can be read before the mark is
invalidated.IllegalArgumentException - if markLimit < 0.IOException - if an error occurs while setting a mark in this reader.markSupported(),
reset()public boolean markSupported()
mark() and
reset() methods. This implementation returns true.markSupported in class Readertrue for BufferedReader.mark(int),
reset()public int read()
throws IOException
read in class ReaderIOException - if this reader is closed or some other I/O error occurs.public int read(char[] buffer,
int offset,
int length)
throws IOException
length characters from this reader and stores them
at offset in the character array buffer. Returns the
number of characters actually read or -1 if the end of the source reader
has been reached. If all the buffered characters have been used, a mark
has not been set and the requested number of characters is larger than
this readers buffer size, BufferedReader bypasses the buffer and simply
places the results directly into buffer.read in class ReaderIndexOutOfBoundsException - if offset < 0 || length < 0 || offset + length > buffer.length.IOException - if this reader is closed or some other I/O error occurs.public String readLine() throws IOException
'\n',
'\r', "\r\n" or the end of the reader. The string does
not include the newline sequence.null if no characters were
read before the end of the reader has been reached.IOException - if this reader is closed or some other I/O error occurs.public boolean ready()
throws IOException
ready in class Readertrue if this reader will not block when read is
called, false if unknown or blocking will occur.IOException - if this reader is closed or some other I/O error occurs.read(),
read(char[], int, int),
readLine()public void reset()
throws IOException
mark() location.
Invocations of read() and skip() will occur from this new
location.reset in class ReaderIOException - if this reader is closed or no mark has been set.mark(int),
markSupported()public long skip(long charCount)
throws IOException
charCount chars in this stream. Subsequent calls to
read will not return these chars unless reset is
used.
Skipping characters may invalidate a mark if markLimit
is surpassed.
skip in class ReaderIllegalArgumentException - if charCount < 0.IOException - if this reader is closed or some other I/O error occurs.Reader.mark(int),
Reader.markSupported(),
Reader.reset()public Stream<String> lines()
Stream, the elements of which are lines read from
this BufferedReader. The Stream is lazily populated,
i.e., read only occurs during the
terminal
stream operation.
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 IOException is thrown when accessing the underlying
BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream
method that caused the read to take place. This method will return a
Stream if invoked on a BufferedReader that is closed. Any operation on
that stream that requires reading from the BufferedReader after it is
closed, will cause an UncheckedIOException to be thrown.
Stream<String> providing the lines of text
described by this BufferedReader