public class LineNumberReader extends BufferedReader
setLineNumber(int) and
getLineNumber() for setting and getting the current line number respectively.
By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().
A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
| Constructor and Description |
|---|
LineNumberReader(Reader
Create a new line-numbering reader, using the default input-buffer size.
|
LineNumberReader(Reader
Create a new line-numbering reader, reading characters into a buffer of the given size.
|
| Modifier and Type | Method and Description |
|---|---|
int |
getLineNumber()
Get the current line number.
|
void |
mark(int readAheadLimit)
Mark the present position in the stream.
|
int |
read()
Read a single character.
|
int |
read(char[] cbuf, int off, int len)
Read characters into a portion of an array.
|
String |
readLine()
Read a line of text.
|
void |
reset()
Reset the stream to the most recent mark.
|
void |
setLineNumber(int lineNumber)
Set the current line number.
|
long |
skip(long n)
Skip characters.
|
close, lines, markSupported, readypublic LineNumberReader(Readerin)
in - A Reader object to provide the underlying stream
public LineNumberReader(Readerin, int sz)
in - A Reader object to provide the underlying stream
sz - An int specifying the size of the buffer
public void setLineNumber(int lineNumber)
lineNumber - An int specifying the line number
getLineNumber()
public int getLineNumber()
setLineNumber(int)
public int read()
throws IOException
read in class
BufferedReader
IOException - If an I/O error occurs
public int read(char[] cbuf,
int off,
int len)
throws IOException
read in class
BufferedReader
cbuf - Destination buffer
off - Offset at which to start storing characters
len - Maximum number of characters to read
IOException - If an I/O error occurs
public StringreadLine() throws IOException
readLine in class
BufferedReader
IOException - If an I/O error occurs
Files.readAllLines(java.nio.file.Path, java.nio.charset.Charset)
public long skip(long n)
throws IOException
skip in class
BufferedReader
n - The number of characters to skip
IOException - If an I/O error occurs
IllegalArgumentException - If
n is negative
public void mark(int readAheadLimit)
throws IOException
mark in class
BufferedReader
readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
IOException - If an I/O error occurs
public void reset()
throws IOException
reset in class
BufferedReader
IOException - If the stream has not been marked, or if the mark has been invalidated