Java FileInputStream available()方法与示例

FileInputStream类的available()方法 (FileInputStream Class available() method)

  • available() method is available in java.io package.

    available()方法在java.io包中可用。

  • available() method is used to return the number of bytes left that can be read from this FileInputStream and without blocking by the next invocation of this method for this FileInputStream.

    available()方法用于返回可以从此FileInputStream读取的剩余字节数,并且不会被此FileInputStream的下一次调用阻塞。

  • 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 while getting any input/output error or when this stream is closed by the close() method.

    IOException :在获取任何输入/输出错误时或通过close()方法关闭此流时,可能引发此异常。

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 available bytes left that can be read from this FileInputStream during unblock.

方法的返回类型为int ,它返回在解除阻塞期间可以从此FileInputStream读取的剩余可用字节数。

Example:

例:

// Java program to demonstrate the example 
// of int available() method 	
// of FileInputStream

import java.io.*;

public class AvailableOfFIS {
 public static void main(String[] args) throws Exception {
  FileInputStream fis_stm = null;
  int count = 0;

  try {
   // Instantiates FileInputStream
   fis_stm = new FileInputStream("D:\\includehelp.txt");

   // Loop to read until available
   // bytes left
   while ((count = fis_stm.read()) != -1) {

    // By using available() method is to
    // return the available bytes to be read
    int avail_bytes = fis_stm.available();

    // Display corresponding bytes value
    byte b = (byte) count;

    // Display value of avail_bytes and b
    System.out.print("fis_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();
   }
  }
 }
}

Output

输出量

fis_stm.available(): 15 : byte: 0
fis_stm.available(): 14 : byte: 4
fis_stm.available(): 13 : byte: 74
fis_stm.available(): 12 : byte: 97
fis_stm.available(): 11 : byte: 118
fis_stm.available(): 10 : byte: 97
fis_stm.available(): 9 : byte: 0
fis_stm.available(): 8 : byte: 8
fis_stm.available(): 7 : byte: 87
fis_stm.available(): 6 : byte: 111
fis_stm.available(): 5 : byte: 114
fis_stm.available(): 4 : byte: 108
fis_stm.available(): 3 : byte: 100
fis_stm.available(): 2 : byte: 33
fis_stm.available(): 1 : byte: 33
fis_stm.available(): 0 : byte: 33


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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值