Java PushbackInputStream available() Example
PushbackInputStream class available() method example. This example shows you how to use available() method.
Syntax is : public int available() throws IOException
This method returns an estimate of the number of bytes that can be read from this input stream without blocking by the next invocation of a method for this input stream.
Here is the code.
/**
* @(#) AvailablePushbackInputStream.java
* A class representing use of method available() of PushbackInputStream
class in java.io Package.
* @Version 10-June-2008
* @author Rose India Team
*/
import java.io.*;
public class AvailablePushbackInputStream {
public static void main(String[] args) throws IOException {
byte[] byteArray = new byte[]{'M', 'A', 'H', 'E', 'N', 'D', 'R', 'A'};
File file = new File("Mahendra.txt");
// Create object of FileOutputStream class for specified file.
FileOutputStream fOutStream = new FileOutputStream(file);
fOutStream.write(byteArray);
// Create object of PushbackInputStream class for specified stream.
InputStream inStream = new FileInputStream(file);
PushbackInputStream pushInStream = new PushbackInputStream(inStream);
// available method call.
int byteCount = pushInStream.available();
System.out.println("Number of bytes that can be read : " + byteCount);
// read content of the file.
System.out.print("Content of the file that can read : ");
for (int c = pushInStream.read(); c != -1; c = pushInStream.read()) {
System.out.print((char) c);
}
}
} |
output of the program.
Number of bytes that can be read : 8
Content of the file that can read : MAHENDRA |
|