方法java.io.InputStream.close()用于关闭此输入流并释放与该流关联的所有系统资源。此方法不需要任何参数,也不返回任何值。此外,发生I / O错误时,将引发IOException。
演示此的程序如下所示-
示例import java.io.FileInputStream;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) throws Exception {
InputStream i = null;
int num = 0;
try {
i = new FileInputStream("C://JavaProgram//data.txt");
num = i.available();
System.out.println("The number of bytes are: " + num);
i.close();
num = i.available();
System.out.println("The number of bytes are: " + num);
} catch(Exception e) {
System.out.print("Error!!! The input stream is closed");
}
}
}
上面程序的输出如下-
输出结果The number of bytes are: 4
Error!!! The input stream is closed