StringBufferInputStream类的available()方法 (StringBufferInputStream Class available() method)
available() method is available in java.io package.
available()方法在java.io包中可用。
available() method is used to get the number of available bytes left that can be read without blocking from this StringBufferInputStream.
available()方法用于获取剩余可用字节数,这些可用字节数可以不受此StringBufferInputStream的影响而被读取。
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 does not throw an exception at the time of returning available bytes.
在返回可用字节时, available()方法不会引发异常。
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 exact number of bytes read.
方法的返回类型为int ,它返回读取的确切字节数。
Example:
例:
// Java program to demonstrate the example
// of int available() method of StringBufferInputStream
import java.io.*;
public class AvailableOfSBIS {
public static void main(String[] args) throws Exception {
StringBufferInputStream is_stm = null;
int count = 0;
try {
// Instantiates StringBufferInputStream
is_stm = new StringBufferInputStream("Java Programming");
// Loop to read until available
// bytes left
while ((count = is_stm.read()) != -1) {
// By using available() method is to
// return the available bytes to be read
int avail_bytes = is_stm.available();
// Display corresponding char value
char ch = (char) count;
// Display value of avail_bytes and ch
System.out.print("is_stm.available(): " + avail_bytes);
System.out.println(" : " + "ch: " + ch);
}
} 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 (is_stm != null) {
is_stm.close();
}
}
}
}
Output
输出量
is_stm.available(): 15 : ch: J
is_stm.available(): 14 : ch: a
is_stm.available(): 13 : ch: v
is_stm.available(): 12 : ch: a
is_stm.available(): 11 : ch:
is_stm.available(): 10 : ch: P
is_stm.available(): 9 : ch: r
is_stm.available(): 8 : ch: o
is_stm.available(): 7 : ch: g
is_stm.available(): 6 : ch: r
is_stm.available(): 5 : ch: a
is_stm.available(): 4 : ch: m
is_stm.available(): 3 : ch: m
is_stm.available(): 2 : ch: i
is_stm.available(): 1 : ch: n
is_stm.available(): 0 : ch: g
翻译自: https://www.includehelp.com/java/stringbufferinputstream-available-method-with-example.aspx