Java - LineNumberInputStream read() method



Description

The Java LineNumberInputStream read() method reads the next byte of data from the input stream. It returns the byte's integer value (0-255) or -1 if the end of the stream is reached. Returns -1 when EOF (End of File) is reached.

Declaration

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

public int read()

Parameters

NA

Return Value

The method returns the next byte of data, or -1 if the end of this stream is reached.

Exception

IOException − If an I/O error occurs.

Example - Usage of LineNumberInputStream read() method

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) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis =null;
      int i;
      char c;
      
      try {
         // create new input stream
         fis = new FileInputStream("test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // read till the end of the file
         while((i = lnis.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // prints
            System.out.print(c);
         }
         
      } 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−

ABCDE

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.

Example - Using read() with skip() to Ignore Initial Bytes

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"))) {
         lnInputStream.skip(5); // Skip the first 5 bytes

         int data;
         while ((data = lnInputStream.read()) != -1) {
            System.out.print((char) data);
         }

         System.out.println("\nFinal Line Number: " + lnInputStream.getLineNumber());

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

Output(if example.txt contains "HelloWorld")

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

World
Final Line Number: 0

Explanation

  • Uses LineNumberInputStream to read "example.txt".

  • Calls skip(5) to ignore the first 5 bytes.

  • Reads remaining bytes one at a time and prints them.

  • At the end, prints the final line number.

java_io_linenumberinputstream.htm