Java中的 ObjectInputStream available() 方法及示例
Java中ObjectInputStream类的available()方法返回在不阻塞流的情况下可以读取的字节数。
语法:
public int available()
参数:此方法不接受任何参数。
返回值:此方法返回可用字节数。
下面的程序说明了上述方法:
方案一:
// Java program to illustrate
// the above method
import java.io.*;
public class GFG {
public static void main(String[] args) throws Exception
{
FileOutputStream out
= new FileOutputStream("gopal.txt");
ObjectOutputStream out1
= new ObjectOutputStream(out);
// write something in the file
out1.writeUTF("Geeks For Geeks");
// Flushes the Stream
out1.flush();
// Closes the stream
out1.close();
// create an ObjectInputStream
// for the file we created before
ObjectInputStream example
= new ObjectInputStream(
new FileInputStream(
"gopal.txt"));
// Print the number of bytes available
System.out.println(example.available());
example.close();
}
}
输出:
参考:https: Java/io/ObjectInputStream.html#available()