【发布时间】:2011-11-12 16:15:14
【问题描述】:
Java 中的许多 I/O 资源(如 InputStream 和 OutputStream)在完成后需要关闭,如 here 所述。
如何在我的项目中搜索未关闭此类资源的地方,例如这种错误:
private void readFile(File file) throws IOException {
InputStream in = new FileInputStream(file);
int nextByte = in.read();
while (nextByte != -1) {
// Do something with the byte here
// ...
// Read the next byte
nextByte = in.read();
}
// Oops! Not closing the InputStream
}
我尝试了一些静态分析工具,例如 PMD 和 FindBugs,但它们并没有将上述代码标记为错误。
【问题讨论】:
-
FindBugs 似乎能够检测到这一点:stackoverflow.com/questions/2570820/…
-
听起来你需要调整 FindBugs 检查的规则
标签: java resources io static-analysis