所以我有创建 ZipInputStream 所需的 FileInputStream,并且我想知道如果 ZipInputStream 发生什么情况>FileInputStream 已关闭。考虑以下代码:
public void Foo(File zip) throws ZipException{
ZipInputStream zis;
FileInputStream fis = new FileInputStream(zip);
try{
zis = new ZipInputStream(fis);
} catch (FileNotFoundException ex) {
throw new ZipException("Error opening ZIP file for reading", ex);
} finally {
if(fis != null){ fis.close();
}
}
zis 剩余内容是否打开? ZipInputStream 对象会发生什么情况?有什么方法可以测试这个吗?
最佳答案
如果您使用的是 java 7,最佳实践是使用“尝试使用资源” block 。 因此资源将自动关闭。
考虑以下示例:
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
关于java - 检查 FileInputStream 是否关闭的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43491060/