Home »
Java programming language
Java LineNumberInputStream available() Method with Example
LineNumberInputStream Class available() method: Here, we are going to learn about the available() method of LineNumberInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 14, 2020
LineNumberInputStream Class available() method
- available() method is available in java.io package.
- available() method is used to return the number of available bytes that can be read from this LineNumberInputStream.
- available() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- available() method may throw an exception at the time of returning available bytes.
IOException: This exception may throw when getting any input/output error while performing.
Syntax:
public int available();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is int, it returns the number of bytes left that can be read from this LineNumberInputStream without blocking.
Example:
// Java program to demonstrate the example
// of int available() method of
// LineNumberInputStream
import java.io.*;
public class AvailableOfLNIS {
public static void main(String[] args) throws Exception {
FileInputStream fis_stm = null;
LineNumberInputStream line_stm = null;
int val = 0;
try {
// Instantiates FileInputStream
fis_stm = new FileInputStream("D:\\includehelp.txt");
line_stm = new LineNumberInputStream(fis_stm);
// Loop to read until available
// bytes left
while ((val = fis_stm.read()) != -1) {
// By using available() method is to
// return the available bytes to be read
int avail_bytes = line_stm.available();
// Display corresponding byte value
byte b = (byte) val;
// Display value of avail_bytes and b
System.out.print("line_stm.available(): " + avail_bytes);
System.out.println(" : " + "byte: " + b);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (fis_stm != null) {
fis_stm.close();
if (line_stm != null) {
line_stm.close();
}
}
}
}
}
Output
line_stm.available(): 1 : byte: 74
line_stm.available(): 1 : byte: 65
line_stm.available(): 0 : byte: 86
line_stm.available(): 0 : byte: 65