Java LineNumberInputStream available()方法与示例

LineNumberInputStream类的available()方法 (LineNumberInputStream 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 that can be read from this LineNumberInputStream.

    available()方法用于返回可从此LineNumberInputStream读取的可用字节数。

  • 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 may throw an exception at the time of returning available bytes.

    available()方法在返回可用字节时可能会引发异常。

    IOException: This exception may throw when getting any input/output error while performing.

    IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。

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 number of bytes left that can be read from this LineNumberInputStream without blocking.

方法的返回类型为int ,它返回可以从此LineNumberInputStream读取而不会阻塞的剩余字节数。

Example:

例:

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

Output

输出量

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


翻译自: https://www.includehelp.com/java/linenumberinputstream-available-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值