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"));
Constructor and Description |
---|
BufferedReader(Reader reader)
Constructs a new BufferedReader on the Reader
in . |
BufferedReader(Reader reader,
int size)
Constructs a new BufferedReader on the Reader
in . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this reader.
|
void |
mark(int markLimit)
Sets a mark position in this reader.
|
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 at most
length characters from this reader and stores them at offset in the character array
buffer . |
java.lang.String |
readLine()
Returns the next line of text available from this reader.
|
void |
reset()
Resets this reader's position to the last
mark() location. |
long |
skip(long amount)
Skips
amount characters in this reader. |
public BufferedReader(Reader reader)
in
. The buffer gets the default size (8 KB).reader
- the Reader that is buffered.public BufferedReader(Reader reader, int size)
in
. The buffer size is specified by the parameter size
.reader
- the Reader that is buffered.size
- the size of the buffer to allocate.java.lang.IllegalArgumentException
- if size <= 0
.public void close()
close
in interface java.lang.AutoCloseable
close
in class AutoCloseable
public void mark(int markLimit)
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.markLimit
- the number of characters that can be read before the mark is invalidated.reset()
public int read()
read
in class Reader
IOException
- if this reader is closed or some other I/O error occurs.public int read(char[] buffer, int offset, int length)
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 Reader
buffer
- the character array to store the characters read.offset
- the initial position in buffer
to store the bytes read from this reader.length
- the maximum number of characters to read, must be non-negative.ProgrammerError
- if offset < 0
or length < 0
, or if offset + length
is
greater than the size of buffer
.public java.lang.String readLine()
'\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.public void reset()
mark()
location. Invocations of read()
and skip()
will occur from this new location.IOException
- if this reader is closed or no mark has been set.mark(int)
public long skip(long amount)
amount
characters in this reader. Subsequent read()
s will not return these characters
unless reset()
is used. Skipping characters may invalidate a mark if markLimit
is surpassed.amount
- the maximum number of characters to skip.java.lang.IllegalArgumentException
- if amount < 0
.IOException
- if this reader is closed or some other I/O error occurs.mark(int)
,
reset()