Java BufferedInputStream available()方法与示例

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

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

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

  • available() method is used to return the number of unread bytes available from an input stream without blocking by the next calling of a method for this InputStream.

    available()方法用于返回输入流中可用的未读字节数,而不会由于下一次为此InputStream的方法的调用而阻塞。

  • 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 checking available bytes.

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

    IOException: This exception may throw while performing input/output operation.

    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 amount of unread unblocking bytes from this input stream.

方法的返回类型为int ,它从该输入流返回未读的未阻塞字节的数量。

Example:

例:

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

import java.io.*;

public class AvailableBAIS {
    public static void main(String[] args) throws Exception {
        byte[] by = {
            97,
            98,
            98,
            99
        };

        // Instantiates ByteArrayInputStream 
        ByteArrayInputStream byte_s = new ByteArrayInputStream(by);

        // To read till EOF
        while (byte_s.available() > 0) {

            // By using available() method is to
            // return the no. of bytes to be left 
            // for reading
            Integer n_byte = byte_s.available();
            System.out.println("Left avail bytes = " + n_byte);

            // Read char value one by one 
            // by using read() from this File
            char ch = (char) byte_s.read();

            // Display value of char
            System.out.println("byte_s.read(): ");
            System.out.println("ch: " + ch);
        }

        // Close the stream 
        byte_s.close();
    }
}

Output

输出量

Left avail bytes = 4
byte_s.read(): 
ch: a
Left avail bytes = 3
byte_s.read(): 
ch: b
Left avail bytes = 2
byte_s.read(): 
ch: b
Left avail bytes = 1
byte_s.read(): 
ch: c


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值