📌  相关文章
📜  Java中的 ByteArrayInputStream available() 方法及示例

📅  最后修改于: 2022-05-13 01:55:30.415000             🧑  作者: Mango

Java中的 ByteArrayInputStream available() 方法及示例

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

语法

public int available()

参数:该函数不接受任何参数。
返回值:该函数返回要读取的输入流中的总字节数。

下面是上述函数的实现:

方案一:

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


Java
// 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 : 5

方案二:

Java

// 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: Java/io/ByteArrayInputStream.html#available()