许多I/O资源,在Java中,如InputStream和OutputStream需要所讨论的,要当他们与完成关闭这里.
如何搜索我的项目中没有关闭此类资源的地方,例如这种错误:
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
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些静态分析工具,如PMD和FindBugs,但是他们没有将上面的代码标记为错误.