Package java.io
Class LineNumberReader
java.lang.Object
java.io.Reader
java.io.BufferedReader
java.io.LineNumberReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
public class LineNumberReader extends BufferedReader
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.-
Field Summary
-
Constructor Summary
Constructors Constructor Description LineNumberReader(Reader in)Constructs a new LineNumberReader on the Readerin.LineNumberReader(Reader in, int size)Constructs a new LineNumberReader on the Readerin. -
Method Summary
Modifier and Type Method Description intgetLineNumber()Returns the current line number for this reader.voidmark(int readlimit)Sets a mark position in this reader.intread()Reads a single character from the source reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int count)Reads up tocountcharacters from the source reader and stores them in the character arraybufferstarting atoffset.StringreadLine()Returns the next line of text available from this reader.voidreset()Resets this reader to the last marked location.voidsetLineNumber(int lineNumber)Sets the line number of this reader to the specifiedlineNumber.longskip(long charCount)SkipscharCountcharacters in this reader.Methods inherited from class java.io.BufferedReader
close, markSupported, ready
-
Constructor Details
-
LineNumberReader
Constructs a new LineNumberReader on the Readerin. The internal buffer gets the default size (8 KB).- Parameters:
in- the Reader that is buffered.
-
LineNumberReader
Constructs a new LineNumberReader on the Readerin. The size of the internal buffer is specified by the parametersize.- Parameters:
in- the Reader that is buffered.size- the size of the buffer to allocate.- Throws:
IllegalArgumentException- ifsize <= 0.
-
-
Method Details
-
getLineNumber
public int getLineNumber()Returns the current line number for this reader. Numbering starts at 0.- Returns:
- the current line number.
-
mark
Sets a mark position in this reader. The parameterreadlimitindicates how many characters can be read before the mark is invalidated. Sendingreset()will reposition this reader back to the marked position, provided thatreadlimithas not been surpassed. The line number associated with this marked position is also stored so that it can be restored whenreset()is called.- Overrides:
markin classBufferedReader- Parameters:
readlimit- the number of characters that can be read from this stream before the mark is invalidated.- Throws:
IOException- if an error occurs while setting the mark in this reader.- See Also:
BufferedReader.markSupported(),reset()
-
read
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'.- Overrides:
readin classBufferedReader- Returns:
- the character read or -1 if the end of the source reader has been reached.
- Throws:
IOException- if the reader is closed or another IOException occurs.
-
read
Reads up tocountcharacters from the source reader and stores them in the character arraybufferstarting atoffset. 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".- Overrides:
readin classBufferedReader- Throws:
IOException- if this reader is closed or another IOException occurs.
-
readLine
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.- Overrides:
readLinein classBufferedReader- Returns:
- the contents of the line or
nullif no characters have been read before the end of the stream has been reached. - Throws:
IOException- if this reader is closed or another IOException occurs.
-
reset
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.- Overrides:
resetin classBufferedReader- Throws:
IOException- if this reader is already closed, no mark has been set or the mark is no longer valid because more thanreadlimitbytes have been read since setting the mark.- See Also:
mark(int),BufferedReader.markSupported()
-
setLineNumber
public void setLineNumber(int lineNumber)Sets the line number of this reader to the specifiedlineNumber. Note that this may have side effects on the line number associated with the last marked position. -
skip
SkipscharCountcharacters in this reader. Subsequent calls toreadwill not return these characters unlessresetis used. This implementation skipscharCountnumber of characters in the source reader and increments the line number count whenever line terminator sequences are skipped.- Overrides:
skipin classBufferedReader- Returns:
- the number of characters actually skipped.
- Throws:
IllegalArgumentException- ifcharCount < 0.IOException- if this reader is closed or another IOException occurs.- See Also:
mark(int),read(),reset()
-