Java ByteArrayInputStream available()方法及示例

Java ByteArrayInputStream available()方法及示例

available() 方法是 Java.io.ByteArrayInputStream 的一个内置方法,返回可以从这个输入流中读取(或跳过)的剩余字节数。它告诉我们可以从输入流中读取的总字节数。

语法:

public int available()

参数 :该函数不接受任何参数。

返回值 :该函数返回要读取的输入流的总字节数。

下面是上述函数的实现。

程序 1 :

// Java program to implement
// the above function
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception
    {
 
        // Array
        byte[] buffer = { 71, 69, 69, 75, 83 };
 
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
 
        // Use the function to get the number
        // of available
        int number = geek.available();
 
        // Print
        System.out.println("Use of available() method : "
                           + number);
    }
}

输出

Use of available() method : 5

程序2

// Java program to implement
// the above function
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception
    {
 
        // Array
        byte[] buffer = { 1, 2, 3, 4 };
 
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
 
        // Use the function to get the number
        // of available
        int number = geek.available();
 
        // Print
        System.out.println("Use of available() method : "
                           + number);
    }
}

输出

Use of available() method : 4

参考资料: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#available()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程