Java - LineNumberInputStream getLineNumber() method



Description

The Java LineNumberInputStream getLineNumber() method returns the current line number in the input stream. The line number starts from 0 by default and increases when a newline character (\n) is encountered. LineNumberInputStream has been deprecated since JDK 1.1 (Recommended alternative: LineNumberReader).

Declaration

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

public int getLineNumber()

Parameters

NA

Return Value

The method returns the current line number.

Exception

NA

Example - Usage of LineNumberInputStream getLineNumber() method

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

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      int i,j;
      char c;
      
      try {
         // create new input streams
         fis = new FileInputStream("test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // reads till the end of the stream
         while((i = lnis.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // if the character is not new line 
            if(i!=10) {
            
               // prints char
               System.out.print("Character read: "+c);
               
               // get the line number
               j = lnis.getLineNumber();
               System.out.println(" at line: "+j);
            }
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

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

Character read: A at line: 0
Character read: B at line: 0
Character read: C at line: 0
Character read: D at line: 0
Character read: E at line: 0

Example - Reading a File and Printing Line Numbers

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

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lnInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         int data;
         while ((data = lnInputStream.read()) != -1) { // Read byte by byte
            if (data == '\n') { // If newline is encountered
               System.out.println("Line Number: " + lnInputStream.getLineNumber());
            }
         }
      } 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 Number: 1
Line Number: 2
Line Number: 3

Explanation

  • Opens example.txt using LineNumberInputStream.

  • Reads one byte at a time using read().

  • Checks for a newline (\n), and prints the current line number using getLineNumber().

  • The line number increases when a new line is encountered.

Example - Manually Setting the Line Number and Reading Data

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

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lnInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         lnInputStream.setLineNumber(100); // Set initial line number to 100

         int data;
         while ((data = lnInputStream.read()) != -1) {
            if (data == '\n') { // If newline is encountered
               System.out.println("Current Line Number: " + lnInputStream.getLineNumber());
            }
         }
      } 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−

Current Line Number: 101
Current Line Number: 102
Current Line Number: 103

Explanation

  • Uses setLineNumber(100) to start line numbering from 100 instead of 0.

  • Reads one byte at a time using read().

  • Prints the line number whenever a newline (\n) is found.

java_io_linenumberinputstream.htm
Advertisements