Java FileInputStream available() Method
Example
Get the number of bytes that are left to read, before and after reading one byte:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Hello World");
myWriter.close();
FileInputStream input = new FileInputStream("filename.txt");
System.out.println(input.available());
input.read();
System.out.println(input.available());
input.close();
}
}
Definition and Usage
The available() method returns the number of bytes that can be read from the stream right now, without waiting (blocking).
For a normal file, this is the number of bytes that are left to read. It gets smaller as you read from the file.
Syntax
public int available()
Parameter Values
None.
Technical Details
| Returns: | The number of bytes that can be read without waiting, as an int. 0 at the end of the file. |
|---|---|
| Throws: | IOException - if the stream is closed, or if the file cannot be read. |
Related Pages
❮ FileInputStream Methods