gpt4 book ai didi

java - “fileInputStream”在退出时未关闭。

转载 作者:行者123 更新时间:2023-12-01 12:13:25 26 4
gpt4 key购买 nike

我已经对我的代码进行了 klocwork 代码分析。我收到以下错误我终于关闭了输入流即使那样我也收到错误“fileInputStream”在退出时未关闭。

下面是一段代码

    LOGGER.log(Level.INFO, "Inside unzipDownloadedFile method");
File fileDirectory = new File(destDir);
FileInputStream fileInputStream = null;
// buffer for read and write data to file
byte[] buffer = new byte[1024];
ZipInputStream zipInputStream = null;
File zipPath = new File(zipFilePath);
FileOutputStream fileOutputStream = null;
// create output directory if it doesn't exist
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
if (zipPath != null) {
if (zipPath.exists()) {
try {
fileInputStream = new FileInputStream(zipFilePath);
zipInputStream = new ZipInputStream(fileInputStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(destDir + File.separator
+ fileName);
// create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
fileOutputStream = new FileOutputStream(newFile);
int zipStream = 0;
while ((zipStream = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, zipStream);
}
fileOutputStream.close();
// close this ZipEntry
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
fileInputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
// close last ZipEntry
if (zipInputStream != null) {
zipInputStream.closeEntry();
zipInputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

最佳答案

这很微妙,但如果仔细观察 finally block ,您会发现如果出现关闭 zipInputStreamfileInputStream 的异常和 fileOutputStream 永远不会关闭。关闭 fileInputStream 的异常将阻止 fileOutputStream 被关闭。即使您的 try 中有一个 fileInputStream.close() ,它也可能无法到达(因为主代码中出现异常)。

} finally {
try {
// close last ZipEntry
if (zipInputStream != null) {
zipInputStream.closeEntry(); // An exception here...
zipInputStream.close(); // Or here...
} // Prevents all of the following from running
if (fileInputStream != null) {
fileInputStream.close(); // Similarly, an exception here...
}
if (fileOutputStream != null) { // Prevents this from running
fileOutputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我建议使用新的 try-with-resources 语句。

<小时/>

您在下面提问:

Is it not possible to use simple try?As i am using java 6.Its the requirement

首先,找到向您提出更改要求的人。也许给他们一份多年日历,并指向 2011 年 7 月 28 日(三年半前),这一天是 Java 6 被 Java 7 取代的日期,然后是 2014 年 3 月 18 日,当时 Java 8 取代了 Java 7。现在至少已经是使用 Java 7 的时候了,而且 Java 7 具有 try-with-resources

但是,是的,当然可以在 Java 1 和 Java 7 之间的 15 年时间内处理这个问题。您只需单独处理关闭的异常,而不是作为一个组来处理。我曾经在 StreamHelper 类中提供了辅助函数。它们看起来像这样:

public static InputStream cleanClose(InputStream in ) {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
// ...do something appropriate, but swallow it...
}
}
return null;
}

...对于 OutputStream 等类似。

然后我会在异常处理程序中使用它,使用以下模式:

InputStream theInput = null;
OutputStream theOutput = null;
try {
theInput = new FileInputStream(/*...*/);
theOutput = new FileOutputStream(/*...*/);

// ...do something with the streams

// Close them normally
theOutput.close();
theOutput = null;
theInput.close();
theInput = null;
}
finally {
// Ensure they're closed
theOutput = StreamHelper.cleanClose(theOutput);
theInput = StreamHelper.cleanClose(theInput);
}

除此之外,这让我可以在详细信息方法上声明异常并允许它们传播,同时仍然在详细信息方法中处理本地流。

不过

try-with-resources 更好,因为在引发另一个异常时清理期间发生的异常成为引发的主要异常的一部分(它们称为“抑制”异常,它们是主要的异常(exception)),而对于像上面这样的旧式代码,您依赖日志记录或您自己的定制机制来了解它们。

关于java - “fileInputStream”在退出时未关闭。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27145456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com