Java.io.LineNumberInputStream.available() Method

Advertisements


Description

The java.io.LineNumberInputStream.available() method returns the number of bytes that can be read from this input stream without blocking.

Declaration

Following is the declaration for java.io.LineNumberInputStream.available() method:

public int available()

Parameters

  • NA

Return Value

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

Exception

  • IOException -- If an I/O error occurs

Example

The following example shows the usage of java.io.LineNumberInputStream.available() 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,j;
      char c;
      
      try{
         // create new input streams
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         
         while((i=lnis.read())!=-1)
         {
            // converts int to char
            c=(char)i;
            
            // prints char
            System.out.println("Character read: "+c);
            
            j=lnis.available();
            
            System.out.println("Available bytes: "+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();     
      }
   }
}

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

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

Character read: A
Available bytes: 2
Character read: B
Available bytes: 1
Character read: C
Available bytes: 1
Character read: D
Available bytes: 0
Character read: E
Available bytes: 0



Advertisements
Advertisements