inputstream示例_Java InputStream available()方法与示例
inputstream示例
InputStream類的available()方法 (InputStream 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 left for reading from this InputStream without blocking by the next call of the method from this InputStream.
available()方法用于返回可從此InputStream讀取的剩余可用字節(jié)數(shù),而不會被該InputStream的下一次調(diào)用阻塞。
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()方法是一種非靜態(tài)方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
available() method may throw an exception at the time of returning available bytes.
available()方法在返回可用字節(jié)時可能會引發(fā)異常。
IOException: This exception may throw when getting any input/output error.
IOException :遇到任何輸入/輸出錯誤時,可能引發(fā)此異常。
Syntax:
句法:
public int available();Parameter(s):
參數(shù):
It does not accept any parameter.
它不接受任何參數(shù)。
Return value:
返回值:
The return type of the method is int, it returns the number of bytes left that can be read.
該方法的返回類型為int ,它返回可以讀取的剩余字節(jié)數(shù)。
Example:
例:
// Java program to demonstrate the example // of int available() method of InputStreamimport java.io.*;public class AvailableOfIS {public static void main(String[] args) throws Exception {InputStream is_stm = null;int val = 0;try {// Instantiates FileInputStreamis_stm = new FileInputStream("D:\\includehelp.txt");// Loop to read until available// bytes leftwhile ((val = is_stm.read()) != -1) {// By using available() method is to// return the available bytes to be readint avail_bytes = is_stm.available();// Display corresponding byte valuebyte b = (byte) val;// Display value of avail_bytes and bSystem.out.print("is_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 streamif (is_stm != null) {is_stm.close();}}} }Output
輸出量
is_stm.available(): 3 : byte: 74 is_stm.available(): 2 : byte: 65 is_stm.available(): 1 : byte: 86 is_stm.available(): 0 : byte: 65翻譯自: https://www.includehelp.com/java/inputstream-available-method-with-example.aspx
inputstream示例
總結(jié)
以上是生活随笔為你收集整理的inputstream示例_Java InputStream available()方法与示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java BigDecimal valu
- 下一篇: mac mysql 默认字符集_MacO