當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java LineNumberInputStream available()用法及代碼示例

LineNumberInputStream類available()方法

  • available() 方法可在java.io包。
  • available() 方法用於返回可以從此 LineNumberInputStream 讀取的可用字節數。
  • available() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • available() 方法可能在返回可用字節時拋出異常。
    IOException:在執行時遇到任何輸入/輸出錯誤時可能會拋出此異常。

用法:

    public int available();

參數:

  • 它不接受任何參數。

返回值:

該方法的返回類型是int,它返回可以從這個 LineNumberInputStream 讀取而不會阻塞的剩餘字節數。

例:

// 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();
    }
   }
  }
 }
}

輸出

line_stm.available():1:byte:74
line_stm.available():1:byte:65
line_stm.available():0:byte:86
line_stm.available():0:byte:65


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java LineNumberInputStream available() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。