Eclipse Java资源泄露问题

3
Eclipse Java警告:资源泄漏:“未分配的可关闭值”从未关闭。
try(FileChannel f = new RandomAccessFile(new File(p),"rw").getChannel();){}

漏洞在哪里?
2个回答

2

这里涉及到的资源是RandomAccessFile。你正在创建这个对象的一个新实例,但你没有将它存储到任何变量中,因此你永远无法关闭它randomAccesFile.close()。


RandomAccessFile f = new RandomAccessFile(new File("a.txt"),"rw"); try(FileChannel ff = f.getChannel();) {} try{f.read();}catch(Exception ex){ex.printStackTrace();} - Sam Adamsh

1
你可以安全地忽略这个。
由于FileChannel也是Closeable,并且FileChannel::close还会关闭底层流。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接