PUBLISHED ON: APRIL 3, 2021
Java FileInputStream available() Method
In this tutorial, we will learn about the available()
method of the FileInputStream class in Java. This method is used to return the number of remaining bytes to be read from the current input stream, without blocking by the next invocation of this method for this FileInputStream. The next method call can also be another thread. It is a non-static method, available in the java.io package.
Syntax
This is the syntax declaration of this method. It does not accept any parameters and it returns the approximate number of bytes left that can be read by this FileInputStream
.
public int available()
Example 1: Check Available Bytes in Java Stream
In this example, we are checking the no. of bytes available in the stream to read using the available method and this method has returned the number 18 it indicates that the total number of remaining bytes to read in the stream is 18.
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
FileInputStream inputStream = new FileInputStream("E:\\studytonight\\file.txt");
DataInputStream dataInputStream = new DataInputStream(inputStream);
int count = inputStream.available();
System.out.println("Total bytes available: "+count);
}
}
Total bytes available: 18
Example 2: Check Available Bytes in Java Stream
Here in this example, we are checking the no. of bytes available in the stream to read using the available method and this method has returned the number 5 it indicates that the total number of remaining bytes to read in the stream are 15.
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
byte[] byte_arr = { 10, 20, 30, 40, 50 };
ByteArrayInputStream byteArrayInputStr = new ByteArrayInputStream(byte_arr);
DataInputStream dataInputStr = new DataInputStream(byteArrayInputStr);
int count = dataInputStr.available();
System.out.println("Total bytes available: "+count);
}
}
Total bytes available: 5
Conclusion:
In this tutorial, we learned about the available()
method of the FileInputStream class in Java, which returns the estimated number of bytes to be read from the current input stream, without blocking by the next invocation of this method for this FileInputStream. This method 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.