LineNumberReader class in java

LineNumberReader class in java

LineNumberReader is a character input stream that keeps track of the number of lines of text that have been read from it. It supports the usual Reader methods, and also the readLine() method introduced by its superclass. In addition to these methods, we can call gettLineNumber() to query the number of lines set so far.

The LineNumberReader class is a BufferedReader that keeps track of line numbers. The line number starts at 0 and is incremented each time an end-of-line character is encountered.

Ads code goes here

LineNumberReader recognizes “\n”, “\r”, or “\r\n” as the end of a line. Regardless of the end-of-line character it reads, ReaderInputStream returns only “\n”. The current line number is returned by getLineNumber().

We can also call setLineNumber() to set the line number for the current line. Subsequent lines are numbered sequentially from this specified starting point.

This class is a character stream analog to LineNumberInputStream, which has been deprecated in Java 1.1. (The LineNumberReader class is the JDK 1.1 replacement for LineNumberInputStream.)

The LineNumberReader is superior as

  • It correctly handles byte to character conversions (via Reader)
  • It implements read(byte[], int, int) and skip() more efficiently

The Class Structure of the LineNumberReader is given as

public class java.io.LineNumberReader extends java.io.BufferedReader
// Public Constructors
public LineNumberReader(Reader in);
public LineNumberReader(Reader in, int size); 
// Public Instance Methods
public int getLineNumber(); 
public void mark(int readAheadlimit) throws IOException;//Overrides BufferedReader 
public int read() throws IOException;//Overrides BufferedReader
public int read(char [ ] cbuf, int off, int len) throws IOException;//Overrides BufferedReader
public String readLine()throws IOException;//Overrides BufferedReader
public void reset() throws IOException;// Overrides BufferedReader
public void setLineNumber(int lineNumber);
public long skip (long n) throws IOException;// Overrides BufferedReader
}

The details of the structure is described as:

public LineNumberReader(Reader in);

public LineNumberReader(Reader in) constructor creates a LineNumberReader that gets its data from in and uses a default sized buffer. The default buffer size for BufferedReader is 8192 characters.
Parameter
in-The reader to use.

public LineNumberReader(Reader in, int size); 

public LineNumberReader(Reader in, int size) constructor creates a LineNumberReader that gets its data from in and uses a buffer of the given size.
Parameter
in– The reader to use.
size– The buffer size.

public int getLineNumber(); 

public int getLineNumber() method returns the current line number.
This method returns the current line number.

public void mark(int readAheadlimit) throws IOException;//Overrides BufferedReader 

public void mark(int readAheadlimit) throws IOException method causes the LineNumberReader to remember its current position. A subsequent call to reset() causes the object to return to that saved position and thus reread a portion of the input.

READ  Class InetAddress in Java

Parameter
readAheadLimit-The maximum number of characters that can be read before the saved position becomes
invalid.

public int read() throws IOException;//Overrides BufferedReader

public int read() throws IOException method reads a character of input from the underlying reader. If “\n”, “\r”, or “\r\n” is read from the stream, “\n” is returned. Otherwise, the character read from the underlying BufferedReader is returned verbatim.

The method blocks until it reads the character, the end of the stream is encountered, or an exception is thrown.

This method returns the next character of data or -1 if the end of the stream is encountered.

public int read(char [ ] cbuf, int off, int len) throws IOException;//Overrides BufferedReader

public int read(char [ ] cbuf, int off, int len) throws IOException method reads up to len characters of input into the given array starting at index off.

This method, unlike read(), returns end-of-line characters exactly as they come from the underlying BufferedReader. The method blocks until data are available.

This method returns the actual number of characters read or -1 if the end of the stream is encountered immediately.

Parameter
cbuf– An array of characters to be filled from the stream.
off– An offset into the array.
len– The number of characters to read.

public String readLine()throws IOException;//Overrides BufferedReader

public String readLine()throws IOException method reads a line of text. Lines are terminated by “\n”, “\r”, or “\r\n”.

The line terminators are not returned with the line string.

This method returns a String containing the line just read, or null if the end of the stream has been reached.

public void reset() throws IOException;// Overrides BufferedReader

public void reset() throws IOException method sets the position of the reader to a position that was saved by a previous call to mark().

READ  Class ObjectStreamClass in Java

Subsequent characters read from this reader will begin from the saved position and continue normally. The method also restores the line number to its correct value for the mark location.

public void setLineNumber(int lineNumber);

public void setLineNumber(int lineNumber) method sets the current line number of the LineNumberReader. The method does not change the position of the reader.

Parameter
lineNumber-The new line number.

public long skip (long n) throws IOException;// Overrides BufferedReader

public long skip (long n) throws IOException method skips n characters of input.

This method returns the actual number of bytes skipped.

Parameter
n-The number of characters to skip.

Apart from these LineNumberReader class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

LineNumberReader class also has inherited methods from BufferedReader. They are as follows:

  • markSupported()
  • ready()
  • close()

LineNumberReader class also has inherited method from Reader. It is as follows:

  • read(char[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *