Skip to content
Related Articles

Related Articles

FileInputStream available() Method in Java with Examples

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 29 Dec, 2021

The available() method of FileInputStream class is used to return the estimated number of remaining bytes that can be read from the input stream without blocking. This method returns the number of bytes remaining to read from the file. When a file is completely read, this function returns zero. 


FileInputStream available() 

Return Value: The method returns and estimates the number of remaining bytes read from this input stream without blocking.

Exception: This method can generate exceptions like IOException or FileNotFoundException.These exceptions are described below.

  • IOException – If the file input stream has been closed by calling close or any I/O error occurs.
  • FileNotFoundException – If that directory is not available, then we will get FileNotFoundException.

How to invoke the available() Method?

Step 1: Attach a file to a FileInputStream as this will enable us to read data from the file as shown below as follows:

FileInputStream fileInputStream = new FileInputStream(“file.txt”);

Step 2: Now, to read data from the file that how much data is available for reading, we should call an available method using FileInputStream object like below;

int ch = fileInputStream.available(); 

Step 3(a): When there is no more data available to read further, the available() method returns 0;  

Step 3(b): Then, we should attach the monitor to the output stream. For displaying the data, we can use System.out.print.  



Original File content: file.txt


This program will read a file and returns how many characters are left to read every time.


// Java program to demonstrate the working
// of the FileInputStream available() method
public class abc {
    public static void main(String[] args)
        // Creating file object and specifying path
        File file = new File("file.txt");
        try {
            FileInputStream input = new FileInputStream(file);
            int character;
            // read character by character by default
            // read() function return int between 0 and 255.
            while ((character = != -1) {
                int ch = input.available();
                System.out.print("Currently Reading:"
                                 + (char)character);
                System.out.print(" Remaining character: "
                                 + ch);
        catch (Exception e) {


Currently Reading:G Remaining character: 12
Currently Reading:e Remaining character: 11
Currently Reading:e Remaining character: 10
Currently Reading:k Remaining character: 9
Currently Reading:s Remaining character: 8
Currently Reading:f Remaining character: 7
Currently Reading:o Remaining character: 6
Currently Reading:r Remaining character: 5
Currently Reading:G Remaining character: 4
Currently Reading:e Remaining character: 3
Currently Reading:e Remaining character: 2
Currently Reading:k Remaining character: 1
Currently Reading:s Remaining character: 0

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!