A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileinputstream_available.htm below:

Java - FileInputStream available() method

Java - FileInputStream available() method Description

The Java FileInputStream available() method returns the number of bytes that can be read from the file input stream without blocking. It does not return the total file size but rather the number of remaining unread bytes in the stream. If available() returns 0, it means the end of the file (EOF) is reached.

Declaration

Following is the declaration for java.io.FileInputStream.available() method −

public int available()
Parameters

NA

Return Value

The methods returns and estimated of the number of remaining bytes that can be read from this input stream without blocking.

Exception

IOException− If the file input stream has been closed by calling close or any I/O error occurs.

Example - Usage of FileInputStream available() method

The following example shows the usage of Java FileInputStream available() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      int available = 0;
      int i = 0;
      
      try {
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the stream
         while((i = fis.read())!=-1) {
         
            // available bytes
            available = fis.available();
            
            // convert integer to character
            char c = (char)i;
            
            // prints
            System.out.print("Available: "+available);
            System.out.println("; Read: "+c);
         }
         
      } catch(Exception ex) {
         // if an I/O error occurs
         ex.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(fis!=null) {
            fis.close();
         }
      }
   }
}
Output Assumption

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

ABCDEF

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

Available: 5; Read: A
Available: 4; Read: B
Available: 3; Read: C
Available: 2; Read: D
Available: 1; Read: E
Available: 0; Read: F
Example - Checking Available Bytes in a File

The following example shows the usage of Java FileInputStream available() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      File file = new File("example.txt");

      try (FileInputStream fis = new FileInputStream(file)) {
         // Get the number of available bytes at the beginning
         int availableBytes = fis.available();
         System.out.println("Bytes available to read: " + availableBytes);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output(if file size is 50 bytes)

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

Bytes available to read: 50
Explanation Example - Checking Available Bytes Before and After Reading

The following example shows the usage of Java FileInputStream available() method.

FileInputStreamDemo.java
package com.tutorialspoint;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      File file = new File("example.txt");

      try (FileInputStream fis = new FileInputStream(file)) {
         // Get available bytes before reading
         System.out.println("Available bytes before reading: " + fis.available());

         // Read 10 bytes from the file
         byte[] buffer = new byte[10];
         fis.read(buffer);

         // Get available bytes after reading 10 bytes
         System.out.println("Available bytes after reading 10 bytes: " + fis.available());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output (if the file size is 50 bytes)

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

Available bytes before reading: 50
Available bytes after reading 10 bytes: 40
Explanation

java_io_fileinputstream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4