StringBufferInputStream類available()方法
- available() 方法可在
java.io
包。 - available() 方法用於從該 StringBufferInputStream 中獲取可以在不阻塞的情況下讀取的剩餘可用字節數。
- available() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- available() 方法在返回可用字節時不拋出異常。
用法:
public int available();
參數:
- 它不接受任何參數。
返回值:
該方法的返回類型是int
,它返回讀取的確切字節數。
例:
// 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();
}
}
}
}
輸出
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
相關用法
- Java StringBufferInputStream reset()用法及代碼示例
- Java StringBufferInputStream skip()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer subSequence()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer setLength()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer insert()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer deleteCharAt(int index)用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java StringBufferInputStream available() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。