Java 中的许多 I/O 资源(例如 InputStream 和 OutputStream)在使用完毕后需要关闭,如前所述here http://www.coderanch.com/t/202922/Performance/java/Unclosed-File-input-stream.
我如何在我的项目中搜索此类资源未关闭的地方,例如这种错误:
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,但它们没有将上述代码标记为错误。
这可能是问题setting- 我通过 IDE 插件运行 FindBugs,它报告 OS_OPEN_STREAM。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)