Java FileInputStream available() Example
FileInputStream class available() method example. This example shows you how to use available() method.
Syntax is : public int available() throws IOException
Method returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
Here is the code.
/**
* @(#) AvailableFileInputStream.java
* A class representing use of method available() of FileInputStream
class in java.io Package.
* @Version 07-June-2008
* @author Rose India Team
*/
import java.io.*;
public class AvailableFileInputStream {
public static void main(String[] args) throws IOException {
//create file object.
File file = new File("C://Mahendra.txt");
int ch;
// Create object of FileInputStream class.
FileInputStream fin = new FileInputStream(file);
// available() method call.
int byteCount = fin.available();
System.out.print("The number of bytes that can be read : " + byteCount);
// Create a empty buffer and append with elements of specified file.
StringBuffer strContent = new StringBuffer("");
while ((ch = fin.read()) != -1) {
strContent.append((char) ch);
}
System.out.print("\nFile contents : ");
System.out.println(strContent);
}
} |
output of the program.
The number of bytes that can be read : 14
File contents : MAHENDRA SINGH
|
|