Java - LineNumberReader readLine() method



Description

The Java LineNumberReader readLine() method reads a single line from the input stream and tracks line numbers automatically. It returns null when the end of the file (EOF) is reached.

Declaration

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

public int readLine()

Parameters

NA

Return Value

The method returns a string containing the contents of the line, not including any line termination characters, or null if the end of the stream is reached.

Exception

IOException − If an I/O error occurs.

Example - Usage of LineNumberReader readLine() method

The following example shows the usage of Java LineNumberReader readLine() 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;
      String str;
      int i;
      
      try {
         // create new reader
         fr = new FileReader("test.txt");
         lnr = new LineNumberReader(fr);
   
         // read lines till the end of the stream
         while((str = lnr.readLine())!=null) {
            i = lnr.getLineNumber();
            System.out.print("("+i+")");
                  
            // prints string
            System.out.println(str);
         }
         
      } 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 multiple lines)

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

(1)ABCDE
(2)FGHIJ
(3)KLMNO
(4)PQRST
(5)UVWXY
(6)z

Example - Reading Lines from a String

The following example shows the usage of Java LineNumberReader readLine() method. This example reads lines from a multi-line string using readLine() and prints each line with its corresponding line number.

LineNumberReaderDemo.java

package com.tutorialspoint;

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

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "Hello World\nJava Programming\nLineNumberReader Example";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

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

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

Output

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

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

Explanation

  • LineNumberReader reads the string line by line.

  • readLine() reads each line and removes the newline (\n).

  • getLineNumber() automatically tracks the current line number.

  • The loop stops when readLine() returns null, indicating EOF.

Example - Reading Lines from a File

The following example shows the usage of Java LineNumberReader readLine() method. This example reads a text file line by line and prints each line along with its 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 {
         // Open a file using LineNumberReader
         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(Assuming example.txt contains multiple lines)

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

Line 1: This is line 1
Line 2: This is line 2
Line 3: This is line 3

Explanation

  • LineNumberReader reads from "example.txt".

  • readLine() reads each line and discards the newline character (\n).

  • getLineNumber() correctly tracks the line number.

  • The loop stops when readLine() returns null (EOF).

java_io_linenumberreader.htm
Advertisements