Java - LineNumberInputStream Class



Introduction

The Java LineNumberInputStream class is an input stream filter that provides the added functionality of keeping track of the current line number. A line is a sequence of bytes ending with a carriage return character ('\r'), a newline character ('\n'), or a carriage return character followed immediately by a linefeed character.

Class declaration

Following is the declaration for Java.io.LineNumberInputStream class −

public class LineNumberInputStream
   extends Reader

Field

Following are the fields for Java.io.LineNumberInputStream class −

  • protected InputStream in − This is the input stream to be filtered.

Class constructors

Sr.No.Constructor & Description
1

LineNumberInputStream(InputStream in)

This constructs a newline number input stream that reads its input from the specified input stream.

Class methods

Sr.No.Method & Description
1int available()

This method returns the number of bytes that can be read from this input stream without blocking.

2int getLineNumber()

This method returns the current line number.

3void mark(int readlimit)

This method marks the current position in this input stream.

4int read()

This method reads the next byte of data from this input stream.

5int read(byte[] b, int off, int len)

This method reads up to len bytes of data from this input stream into an array of bytes.

6void reset()

This method repositions this stream to the position at the time the mark method was last called on this input stream.

7void setLineNumber(int lineNumber)

This method sets the line number to the specified argument.

8long skip(long n)

This method skips over and discards n bytes of data from this input stream.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.FilterInputStream
  • Java.io.Object

Example - Checking Available Bytes Before Reading

The following example shows the usage of Java LineNumberInputStream available() 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 lineNumberInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Bytes available: " + lineNumberInputStream.available());

         // Read the first byte
         int data = lineNumberInputStream.read();
         System.out.println("First byte read: " + (char) data);

         // Check available bytes again
         System.out.println("Bytes available after reading one byte: " + lineNumberInputStream.available());

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

Output(if example.txt contains "Hello")

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

Bytes available: 5
First byte read: H
Bytes available after reading one byte: 4

Explanation

  • Uses LineNumberInputStream to read from "example.txt".

  • Calls available() before reading, showing the total available bytes.

  • Reads one byte, then calls available() again to show the remaining unread bytes.

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 - Reading a File Byte by Byte and Tracking Line Numbers

The following example shows the usage of Java LineNumberInputStream read() 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
            System.out.print((char) data); // Convert byte to char and print

            if (data == '\n') { // Check if a new line 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−

Hello
Line Number: 1
Java
Line Number: 2

Explanation

  • Uses LineNumberInputStream to read "example.txt".

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

  • Converts each byte into a character and prints it.

  • Whenever a newline (\n) is encountered, prints the current line number.