java.lang.Object | |||
↳ | java.io.Reader | ||
↳ | java.io.BufferedReader | ||
↳ | java.io.LineNumberReader |
Wraps an existing Reader
and counts the line terminators encountered
while reading the data. The line number starts at 0 and is incremented any
time '\r'
, '\n'
or "\r\n"
is read. The class has an
internal buffer for its data. The size of the buffer defaults to 8 KB.
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new LineNumberReader on the Reader
in . | |||||||||||
Constructs a new LineNumberReader on the Reader
in . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the current line number for this reader.
| |||||||||||
Sets a mark position in this reader.
| |||||||||||
Reads a single character from the source reader and returns it as an
integer with the two higher-order bytes set to 0.
| |||||||||||
Reads at most
count characters from the source reader and stores
them in the character array buffer starting at offset . | |||||||||||
Returns the next line of text available from this reader.
| |||||||||||
Resets this reader to the last marked location.
| |||||||||||
Sets the line number of this reader to the specified
lineNumber . | |||||||||||
Skips
count number of characters in this reader. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() | |||||||||||
![]() |
Constructs a new LineNumberReader on the Reader in
. The internal
buffer gets the default size (8 KB).
in | the Reader that is buffered. |
---|
Constructs a new LineNumberReader on the Reader in
. The size of
the internal buffer is specified by the parameter size
.
in | the Reader that is buffered. |
---|---|
size | the size of the buffer to allocate. |
Public Methods
public
int
getLineNumber
()
Since: API Level 1
Returns the current line number for this reader. Numbering starts at 0.
Returns
the current line number.
public
void
mark
(int readlimit)
Since: API Level 1
Sets a mark position in this reader. The parameter
readlimit
indicates how many characters can be read before the mark is invalidated.
Sending reset()
will reposition this reader back to the marked
position, provided that readlimit
has not been surpassed. The
line number associated with this marked position is also stored so that
it can be restored when reset()
is called.
Parameters
readlimit
| the number of characters that can be read from this stream
before the mark is invalidated. |
---|
public
int
read
()
Since: API Level 1
Reads a single character from the source reader and returns it as an
integer with the two higher-order bytes set to 0. Returns -1 if the end
of the source reader has been reached.
The line number count is incremented if a line terminator is encountered.
Recognized line terminator sequences are
'\r'
, '\n'
and
"\r\n"
. Line terminator sequences are always translated into
'\n'
.
Returns
the character read or -1 if the end of the source reader has been
reached.
public
int
read
(char[] buffer, int offset, int count)
Since: API Level 1
Reads at most
count
characters from the source reader and stores
them in the character array buffer
starting at offset
.
Returns the number of characters actually read or -1 if no characters
have been read and the end of this reader has been reached.
The line number count is incremented if a line terminator is encountered.
Recognized line terminator sequences are
'\r'
, '\n'
and
"\r\n"
.
Parameters
buffer
| the array in which to store the characters read. |
---|---|
offset
| the initial position in |
count
| the maximum number of characters to store in |
Returns
the number of characters actually read or -1 if the end of the
source reader has been reached while reading.
public
String
readLine
()
Since: API Level 1
Returns the next line of text available from this reader. A line is
represented by 0 or more characters followed by
'\r'
,
'\n'
, "\r\n"
or the end of the stream. The returned
string does not include the newline sequence.
Returns
the contents of the line or null
if no characters have
been read before the end of the stream has been reached.
public
void
reset
()
Since: API Level 1
Resets this reader to the last marked location. It also resets the line
count to what is was when this reader was marked. This implementation
resets the source reader.
Throws
IOException
| if this reader is already closed, no mark has been set or the
mark is no longer valid because more than |
---|
public
void
setLineNumber
(int lineNumber)
Since: API Level 1
Sets the line number of this reader to the specified
lineNumber
.
Note that this may have side effects on the line number associated with
the last marked position.
Parameters
lineNumber
| the new line number value. |
---|
public
long
skip
(long count)
Since: API Level 1
Skips
count
number of characters in this reader. Subsequent
read()
's will not return these characters unless reset()
is used. This implementation skips count
number of characters in
the source reader and increments the line number count whenever line
terminator sequences are skipped.
Parameters
count
| the number of characters to skip. |
---|
Returns
the number of characters actually skipped.
Throws
IllegalArgumentException
| if |
---|---|
IOException
| if this reader is closed or another IOException occurs. |