我正在尝试使用 findBugs 在我们的遗留代码中查找错误。在一种方法中,findBugs 给出了 OBL_UNSATISFIED_OBLIGATION 错误。我已验证所有流均已正确关闭。这是代码片段:
FileWriter fw = null;
FileReader fr = null;
try {
if (!new File(filePath).exists()) {
requiredStrings = CommandUtils.invoke(filename);
fw = new FileWriter(filePath);
fw.write(requiredStrings);
} else {
StringBuilder sb = new StringBuilder();
fr = new FileReader(filePath);
char[] buffer = new char[BLOCK_READ_SIZE];
int bytesRead;
while (-1 != (bytesRead = fr.read(buffer, 0, BLOCK_READ_SIZE))) {
sb.append(buffer, 0, bytesRead);
}
requiredStrings = sb.toString();
}
} finally {
if (fw != null) {
fw.close();
}
if (fr != null) {
fr.close();
}
}
return requiredStrings;
该错误表明清理资源的义务未解除,路径继续在....行.... 剩余义务 {Reader x 1, Writer x-1}