Java ByteArrayInputStream close()方法及示例
close() 方法是 Java.io.ByteArrayInputStream 的一个内置方法,它关闭了输入流并将与此流相关的系统资源释放给垃圾收集器。
语法:
public void close()
参数 :该函数不接受任何参数。
返回值 :该函数不返回任何东西。
下面是上述函数的实现。
程序 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 = { 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);
// Closes the InputStream
geek.close();
}
}
输出。
Use of available() method : 4
程序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 = { 2, 3, 4, 8, 9 };
// 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);
// Closes the InputStream
geek.close();
}
}
输出。
Use of available() method : 5
参考资料: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#close()
极客教程