LineNumberInputStream类的available()方法 (LineNumberInputStream Class available() method)
available() method is available in java.io package.
available()方法在java.io包中可用。
available() method is used to return the number of available bytes that can be read from this LineNumberInputStream.
available()方法用于返回可从此LineNumberInputStream读取的可用字节数。
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 available bytes.
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 the number of bytes left that can be read from this LineNumberInputStream without blocking.
方法的返回类型为int ,它返回可以从此LineNumberInputStream读取而不会阻塞的剩余字节数。
Example:
例:
// Java program to demonstrate the example
// of int available() method of
// LineNumberInputStream
import java.io.*;
public class AvailableOfLNIS {
public static void main(String[] args) throws Exception {
FileInputStream fis_stm = null;
LineNumberInputStream line_stm = null;
int val = 0;
try {
// Instantiates FileInputStream
fis_stm = new FileInputStream("D:\\includehelp.txt");
line_stm = new LineNumberInputStream(fis_stm);
// Loop to read until available
// bytes left
while ((val = fis_stm.read()) != -1) {
// By using available() method is to
// return the available bytes to be read
int avail_bytes = line_stm.available();
// Display corresponding byte value
byte b = (byte) val;
// Display value of avail_bytes and b
System.out.print("line_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 (line_stm != null) {
line_stm.close();
}
}
}
}
}
Output
输出量
line_stm.available(): 1 : byte: 74
line_stm.available(): 1 : byte: 65
line_stm.available(): 0 : byte: 86
line_stm.available(): 0 : byte: 65
翻译自: https://www.includehelp.com/java/linenumberinputstream-available-method-with-example.aspx