int available()
描述 (Description)
java.io.PushbackInputStream.available()方法返回可以从此输入流中读取(或跳过)的字节数的估计值,而不会被下一次调用此输入流的方法阻塞。 下一次调用可能是同一个线程或另一个线程。 单个读取或跳过这么多字节不会阻塞,但可以读取或跳过更少的字节。
声明 (Declaration)
以下是java.io.PushbackInputStream.available()方法的声明。
public int available()
参数 (Parameters)
NA
返回值 (Return Value)
此方法返回可以从输入流中无阻塞地读取(或跳过)的字节数。
异常 (Exception)
IOException - 如果通过调用close()方法关闭此输入流,或发生I/O错误。
例子 (Example)
以下示例显示了java.io.PushbackInputStream.available()方法的用法。
package com.iowiki;
import java.io.*;
public class PushbackInputStreamDemo {
public static void main(String[] args) {
// declare a buffer and initialize its size:
byte[] arrByte = new byte[1024];
// create an array for our message
byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o'};
try {
// create object of PushbackInputStream class for specified stream
InputStream is = new ByteArrayInputStream(byteArray);
PushbackInputStream pis = new PushbackInputStream(is);
// check how many bytes are available
System.out.println("" + pis.available());
// read from the buffer one character at a time
for (int i = 0; i < byteArray.length; i++) {
// read a char into our array
arrByte[i] = (byte) pis.read();
// display the read byte
System.out.print((char) arrByte[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
5
Hello