FilterInputStream类的available()方法 (FilterInputStream Class available() method)
available() method is available in java.io package.
available()方法在java.io包中可用。
available() method is used to return the available bytes left that can be read from this FilterIputStream without blocking by the next call of this method for this FilterInputStream.
available()方法用于返回可以从此FilterIputStream读取的剩余可用字节,而不会被此FilterInputStream的此方法的下一次调用阻塞。
available() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
available()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
available() method may throw an exception at the time of returning left bytes to be read.
在返回要读取的左字节时, available()方法可能会引发异常。
IOException: This exception may throw when getting any input/output error while performing.
IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。
Syntax:
句法:
public int available();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is int, it returns an approximate number of available bytes that can be read.
方法的返回类型为int ,它返回大约可以读取的可用字节数。
Example:
例:
// Java program to demonstrate the example
// of int available() method of FilterInputStream
import java.io.*;
public class AvailableOfFIS {
public static void main(String[] args) throws Exception {
FileInputStream fis_stm = null;
FilterInputStream fil_stm = null;
int count = 0;
try {
// Instantiates FileInputStream and
// FilterInputStream
fis_stm = new FileInputStream("D:\\includehelp.txt");
fil_stm = new BufferedInputStream(fis_stm);
// Loop to read until available
// bytes left
while ((count = fil_stm.read()) != -1) {
// By using available() method is to
// return the available bytes to be read
int avail_bytes = fil_stm.available();
// Display corresponding bytes value
byte b = (byte) count;
// Display value of avail_bytes and b
System.out.print("fil_stm.available(): " + avail_bytes);
System.out.println(" : " + "byte: " + b);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (fis_stm != null) {
fis_stm.close();
if (fil_stm != null) {
fil_stm.close();
}
}
}
}
}
Output
输出量
fil_stm.available(): 15 : byte: 74
fil_stm.available(): 14 : byte: 97
fil_stm.available(): 13 : byte: 118
fil_stm.available(): 12 : byte: 97
fil_stm.available(): 11 : byte: 32
fil_stm.available(): 10 : byte: 87
fil_stm.available(): 9 : byte: 111
fil_stm.available(): 8 : byte: 114
fil_stm.available(): 7 : byte: 108
fil_stm.available(): 6 : byte: 100
fil_stm.available(): 5 : byte: 46
fil_stm.available(): 4 : byte: 46
fil_stm.available(): 3 : byte: 46
fil_stm.available(): 2 : byte: 46
fil_stm.available(): 1 : byte: 46
fil_stm.available(): 0 : byte: 46
翻译自: https://www.includehelp.com/java/filterinputstream-available-method-with-example.aspx