提问人:Antoine 提问时间:1/28/2019 更新时间:1/28/2019 访问量:131
Eclipse 中文件编写器的不必要资源泄漏警告
Unnecessary resource leak warning in Eclipse for file writer
问:
我遍历了预期具有相同行数的文件行:
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFile)));
BufferedReader[] brs = new BufferedReader[inputFiles.length];
for (int i = 0; i < inputFiles.length; i++) {
brs[i] = Files.newBufferedReader(Paths.get(inputFiles[i]), StandardCharsets.UTF_8);
}
String[] lines = new String[inputFiles.length];
boolean shouldContinue = true;
while (shouldContinue) {
// read all next lines
for (int i = 0; i < inputFiles.length; i++) {
lines[i] = brs[i].readLine();
if (lines[i] == null) {
shouldContinue = false;
}
}
// sanity check
if (!shouldContinue) {
for (String line : lines) {
if (line != null) {
for (int i = 0; i < inputFiles.length; i++) {
brs[i].close();
}
writer.close();
throw new RuntimeException("All files should contain the same number of lines!");
}
}
break;
}
// processing the lines
}
但是,我收到 Eclipse Mars 针对异常抛出行的以下警告:
潜在的资源泄漏:“writer”可能不会在此位置关闭
我做错了什么吗?以及如何解决它?
答:
0赞
Ori Marko
1/28/2019
#1
Surrond 尝试使用资源
try (BufferedWriter writer = new
BufferedWriter(new FileWriter(new File(outputFile)))){
//code
} catch (Exception e) { // handle exception
}
评论
0赞
Antoine
1/28/2019
无济于事.
0赞
emilles
1/28/2019
你是打开作家和许多读者。您需要关闭快乐路径和异常路径中的所有内容,否则警告是合法的。
评论
brs[i].close();
writer.close();
writer
brs
writer