我想知道在FileChannel中IOUtils.closeQuitely做得很好的Apache库方法。我看到它接受Closeable作为参数,而FileChannel确实在层次结构中实现了它。但是,我们是否可以面对任何问题。请分享任何可以分享的经验。
发布于 2013-02-15 14:10:56
我认为如果我们按照IOUtils.closeQuietly API建议的那样使用它是可以的,即双击
FileChannel ch = null;
try {
ch = new FileInputStream("foo.txt").getChannel();
// process
ch.close();
} catch (Exception e) {
// error handling
} finally {
IOUtils.closeQuietly(ch);
}但是,如果Java 7可用,尝试使用资源是一种更干净的方式。
https://stackoverflow.com/questions/14889083
复制相似问题