Java - LineNumberReader getLineNumber() method



Description

The Java LineNumberReader getLineNumber() returns the current line number while reading from a text source.

Declaration

Following is the declaration for java.io.LineNumberReader.getLineNumber() method −

public int getLineNumber()

Parameters

NA

Return Value

The method returns the current line number.

Exception

NA

Example - Usage of LineNumberReader getLineNumber() method

The following example shows the usage of Java LineNumberReader getLineNumber() method.

LineNumberReaderDemo.java

package com.tutorialspoint;

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

public class LineNumberReaderDemo {
   public static void main(String[] args) throws IOException {      
      FileReader fr = null;
      LineNumberReader lnr = null;
      int i;
      
      try {
         // create new reader
         fr = new FileReader("test.txt");
         lnr = new LineNumberReader(fr);
         
         // sets the current line number
         lnr.setLineNumber(100);
         
         // get the current line number
         i = lnr.getLineNumber();
         
         // print the current line number
         System.out.print("Current line number: "+i);
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases system resources
         if(fr!=null)
            fr.close();
         if(lnr!=null)
            lnr.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Current line number: 100

Example - Reading a File with Line Numbers

The following example shows the usage of Java LineNumberReader getLineNumber() method. This example reads a text file line by line and prints the current line number.

LineNumberReaderDemo.java

package com.tutorialspoint;

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

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      try {
         // Create a LineNumberReader from a file
         LineNumberReader lnr = new LineNumberReader(new FileReader("example.txt"));

         String line;
         while ((line = lnr.readLine()) != null) {
            System.out.println("Line " + lnr.getLineNumber() + ": " + line);
         }

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains multiple lines.)

Let us compile and run the above program, this will produce the following result−

Line 1: Hello
Line 2: World
Line 3: Java Programming

Explanation

  • LineNumberReader reads lines from "example.txt".

  • readLine() reads a line, and getLineNumber() returns the current line number.

  • The line number starts at 1 and increments automatically.

Example - Setting and Getting Line Numbers

The following example shows the usage of Java LineNumberReader getLineNumber() method. This example manually sets the line number using setLineNumber(int) and retrieves it with getLineNumber().

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "First Line\nSecond Line\nThird Line";
      BufferedReader br = new BufferedReader(new StringReader(text));
      LineNumberReader lnr = new LineNumberReader(br);

      try {
         lnr.setLineNumber(100); // Set starting line number to 100

         System.out.println("Before reading, Line Number: " + lnr.getLineNumber());

         System.out.println(lnr.readLine());
         System.out.println("After first read, Line Number: " + lnr.getLineNumber());

         System.out.println(lnr.readLine());
         System.out.println("After second read, Line Number: " + lnr.getLineNumber());

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Before reading, Line Number: 100
First Line
After first read, Line Number: 101
Second Line
After second read, Line Number: 102

Explanation

  • A LineNumberReader reads from a string source.

  • setLineNumber(100) manually sets the starting line number.

  • getLineNumber() returns the current line number before and after reading each line.

  • The line number increases automatically as lines are read.

java_io_linenumberreader.htm
Advertisements