- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经对我的代码进行了 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 ,您会发现如果出现关闭 zipInputStream
、fileInputStream
的异常和 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/
我目前正在为 Cocoa/Objective-C 项目编写一个脚本来完全自动化我的编译-运行-调试过程。 我的最后一行代码是: lldb -f Build/MyApp.app -o "run" 这实际
我有一个带有登录屏幕的脚本,如果按下取消按钮,我想完全退出该应用程序。我尝试了 3 种方法: 系统退出() QApplication.quit() QCoreApplication.instance(
我有一个 Flash 应用程序,可以重定向到另一个页面。我很乐意捕获任何其他窗口卸载事件(单击链接/提交表单)并警告用户他们将丢失 Flash 应用程序中的进度。 但是,我找不到任何方法来判断 URL
我正在尝试在 Ubuntu 上用 Python 编写一个简单的程序,它将在播放视频完成后关闭/退出/退出 VLC Player。 能否请您指导我应该在我的程序中添加什么以获得我需要的结果。 impor
我在 Lynda.com 上学习 PHP 2 视频时遇到了一个问题,因为讲师似乎忽略了告诉我们他在视频中执行的步骤之一。我在这里上传了相关视频http://www.youtube.com/watch?
某天在群内有同学问到,在python下我用input或者raw_input都得输入完后回车才能获取到输入的值,那如何实现任意键退出暂停等功能呢,我当时也没有多想,因为接触python时间也不算长,主
我按顺序调用了几个函数(我无法编辑),但有些函数会重定向用户,所以我永远不会进入下一个函数。 我正在调用一个第三方函数,它调用了我能够阻止的 wp_redirect(),但是下一行是 exit;我不知
终止/退出主函数的 D 方式是什么? import std.stdio; import core.thread; void main() { int i; while (i <= 5)
我正在申请写作。用户可以打开应用程序、写一些文本、保存他们的工作等。 我试图做到这一点,以便点击 window close按钮将提示用户 (a) 保存他们的工作(如有必要)或 (b) 退出。 我正在尝
我正在通过在 repl 中检查别人的代码来玩弄它。 它不断调用 System/exit,这导致我的 repl 崩溃。这真是令人气愤。 在我有权访问的所有代码中,我都模拟了调用。 但它也会调用一些我没有
我正在使用 subprocess执行mimic的模块程序(指定 here )。下面的代码成功地读取了一些文本并写入了一个 mp3 文件。 import subprocess proc = subpro
退出 .then 范围后数组上的值被清除 在下面的代码中tableValues1.length 给我正确的长度,直到它位于每个循环内当它退出时,作用域数组长度为零。 请谁能帮我解决这个问题 - 谢谢
我正在尝试为 s3cmd 编写一个 docker 镜像。当我通过 docker-compose 运行从 Dockerfile 构建的图像时,容器在 docker compose run 命令之前退出。
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Quitting an application - is that frowned upon? 我编写了一个 And
我遇到 Selenium WebDriver 仅执行一次后退出 for 循环的问题。据推测,这是获取内容和在加载页面之前启动循环的问题。是否有可能让 webdriver 等待页面加载? List al
#include #include #include #include "Player.h" using namespace std; void PlayerMenu(); int main()
class Test{ public static void main(String args[]) { Patron list[] = new PatronData(
我正在做一些作业,遇到了这个问题。 Write a program that reads several lines of text and prints a table indicating the
我正在用 C 创建一个简单的 Linux 命令 shell。我无法理解我的代码在哪里出现问题。 “commands”是我希望作为一个父进程的子进程同时执行的 Linux 命令字符串列表。当所有执行完成
我的控制台应用程序有点问题。应用程序应该从用户那里获取数字并将它们添加到列表中,但是如果输入是“c”,它应该关闭。我不知道如何在不使用 Scanner.nextLine() 挂起应用程序并退出循环的情
我是一名优秀的程序员,十分优秀!