o7planning

Java LineNumberReader Tutorial with Examples

  1. LineNumberReader
  2. setLineNumber(int)

1. LineNumberReader

LineNumberReader is a subclass of BufferedReader, which supports tracking the line number of lines of text. Line numbering starts from zero.
Whenever LineNumberReader encounters a line break, the line number will increase by 1. The getLineNumber() method returns the current line number. You can also use the setLineNumber(int) method to set the line number for the current line of text.
LineNumberReader constructors
LineNumberReader(Reader in)

LineNumberReader(Reader in, int sz)
test.txt
Java Tutorials:

Java Reader Tutorial
Java Writer Tutorial
Example:
LineNumberReaderEx1.java
package org.o7planning.linenumberreader.ex;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;

public class LineNumberReaderEx1 {
    
    public static void main(String[] args) throws IOException  {  

        try(Reader reader = new FileReader("/Volumes/Data/test/test.txt");
                LineNumberReader lineNumberReader = new LineNumberReader(reader)){
            
            System.out.println("Start Line Number: " + lineNumberReader.getLineNumber());
            
            System.out.println(" ----- ");

            String line = null; ;
            while((line  = lineNumberReader.readLine()) != null) {  
                
                System.out.println("Line Number: " + lineNumberReader.getLineNumber());
                System.out.println("  Line Content: " + line);
            }
        }
    }  
}
Output:
Start Line Number: 0
 -----
Line Number: 1
  Line Content: Java Tutorials:
Line Number: 2
  Line Content:
Line Number: 3
  Line Content: Java Reader Tutorial
Line Number: 4
  Line Content: Java Writer Tutorial

2. setLineNumber(int)

Set the line number for the current line of text.
public void setLineNumber(int lineNumber)
Example:
test2.txt
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
LineNumberReader_setLineNumber_ex1.java
package org.o7planning.linenumberreader.ex;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;

public class LineNumberReader_setLineNumber_ex1 {
    
    public static void main(String[] args) throws IOException  {  

        try(Reader reader = new FileReader("/Volumes/Data/test/test2.txt");
                LineNumberReader lineNumberReader = new LineNumberReader(reader)){

            System.out.println("Set current line number to 2");
            
            lineNumberReader.setLineNumber(2);
            
            System.out.println("Line Number after set: " + lineNumberReader.getLineNumber());
            
            System.out.println("  -----  ");
            
            String line = null; ;
            while((line  = lineNumberReader.readLine()) != null) {  
                
                System.out.println("Line Number: " + lineNumberReader.getLineNumber());
                System.out.println("  Line Content: " + line);
            }
        }
    }  
}
Output:
Set current line number to 2
Line Number after set: 2
  -----  
Line Number: 3
  Line Content: AAAAA
Line Number: 4
  Line Content: BBBBB
Line Number: 5
  Line Content: CCCCC
Line Number: 6
  Line Content: DDDDD
Line Number: 7
  Line Content: EEEEE

Java IO Tutorials

Show More