java.lang.Object | ||
↳ | java.io.Reader | |
↳ | java.io.BufferedReader |
![]() |
Wraps an existing 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.
BufferedReader buf = new BufferedReader(new FileReader("file.java"));
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
BufferedReader , providing in with a buffer
of 8192 characters. | |||||||||||
Constructs a new
BufferedReader , providing in with size characters
of buffer. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Closes this reader.
| |||||||||||
Sets a mark position in this reader.
| |||||||||||
Indicates whether this reader supports the
mark() and
reset() methods. | |||||||||||
Reads a single character from this reader and returns it with the two
higher-order bytes set to 0.
| |||||||||||
Reads at most
length characters from this reader and stores them
at offset in the character array buffer . | |||||||||||
Returns the next line of text available from this reader.
| |||||||||||
Indicates whether this reader is ready to be read without blocking.
| |||||||||||
Resets this reader's position to the last
mark() location. | |||||||||||
Skips
amount characters in this reader. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
Constructs a new BufferedReader
, providing in
with a buffer
of 8192 characters.
in | the Reader the buffer reads from.
|
---|
Constructs a new BufferedReader
, providing in
with size
characters
of buffer.
in | the InputStream the buffer reads from. |
---|---|
size | the size of buffer in characters. |
Public Methods
public
void
close
()
Since: API Level 1
Closes this reader. This implementation closes the buffered source reader
and releases the buffer. Nothing is done if this reader has already been
closed.
public
void
mark
(int markLimit)
Since: API Level 1
Sets a mark position in this reader. The parameter
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.
Parameters
markLimit
| the number of characters that can be read before the mark is
invalidated. |
---|
Throws
IllegalArgumentException
| if |
---|---|
IOException
| if an error occurs while setting a mark in this reader. |
public
boolean
markSupported
()
Since: API Level 1
Indicates whether this reader supports the
mark()
and
reset()
methods. This implementation returns true
.
Returns
true
for BufferedReader
.
public
int
read
()
Since: API Level 1
Reads a single character from this reader and returns it with the two
higher-order bytes set to 0. If possible, BufferedReader returns a
character from the buffer. If there are no characters available in the
buffer, it fills the buffer and then returns a character. It returns -1
if there are no more characters in the source reader.
Returns
the character read or -1 if the end of the source reader has been
reached.
public
int
read
(char[] buffer, int offset, int length)
Since: API Level 1
Reads at most
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
.
Parameters
buffer
| the character array to store the characters read. |
---|---|
offset
| the initial position in |
length
| the maximum number of characters to read, must be
non-negative. |
Returns
number of characters read or -1 if the end of the source reader
has been reached.
Throws
IndexOutOfBoundsException
| if |
---|---|
IOException
| if this reader is closed or some other I/O error occurs.
|
public
String
readLine
()
Since: API Level 1
Returns the next line of text available from this reader. A line is
represented by zero or more characters followed by
'\n'
,
'\r'
, "\r\n"
or the end of the reader. The string does
not include the newline sequence.
Returns
the contents of the line or null
if no characters were
read before the end of the reader has been reached.
public
boolean
ready
()
Since: API Level 1
Indicates whether this reader is ready to be read without blocking.
Returns
true
if this reader will not block when read
is
called, false
if unknown or blocking will occur.
public
void
reset
()
Since: API Level 1
Resets this reader's position to the last
mark()
location.
Invocations of read()
and skip()
will occur from this new
location.
public
long
skip
(long amount)
Since: API Level 1
Skips
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.
Parameters
amount
| the maximum number of characters to skip. |
---|
Returns
the number of characters actually skipped.
Throws
IllegalArgumentException
| if |
---|---|
IOException
| if this reader is closed or some other I/O error occurs. |