FilterInputStream類available()方法
- available() 方法可在
java.io
包。 - available() 方法用於返回可以從此 FilterIputStream 讀取的剩餘可用字節,而不會被此 FilterInputStream 的此方法的下一次調用阻塞。
- available() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
- available() 方法可能會在返回要讀取的左字節時拋出異常。
IOException:在執行時遇到任何輸入/輸出錯誤時可能會拋出此異常。
用法:
public int available();
參數:
- 它不接受任何參數。
返回值:
該方法的返回類型是int
,它返回可以讀取的近似可用字節數。
例:
// 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();
}
}
}
}
}
輸出
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
相關用法
- Java FilterInputStream markSupported()用法及代碼示例
- Java FilterInputStream mark()用法及代碼示例
- Java FilterInputStream close()用法及代碼示例
- Java FilterInputStream reset()用法及代碼示例
- Java FilterInputStream skip()用法及代碼示例
- Java FilterReader reset()用法及代碼示例
- Java FilterOutputStream flush()用法及代碼示例
- Java FilterReader skip()用法及代碼示例
- Java FilterWriter close()用法及代碼示例
- Java FilterReader close()用法及代碼示例
- Java FilterOutputStream close()用法及代碼示例
- Java FilterReader ready()用法及代碼示例
- Java FilterReader markSupported()用法及代碼示例
- Java FilterReader mark()用法及代碼示例
- Java FilterWriter flush()用法及代碼示例
- Java File isHidden()用法及代碼示例
- Java File renameTo()用法及代碼示例
- Java File setWritable()用法及代碼示例
- Java File listFiles()用法及代碼示例
- Java FileStore getBlockSize()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java FilterInputStream available() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。