Java.io.LineNumberInputStream.read() Method

Advertisements


Description

The java.io.LineNumberInputStream.read() reads the next byte of data from this input stream. The value byte is returned as an int in the range of 0 to 255. The method returned -1, if the end of the stream has been 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

The following example shows the usage of java.io.LineNumberInputStream.read() method.

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("C:/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();      
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program:

ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
z

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

ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
z



Advertisements
Advertisements