我需要关闭FileOutputStream对象吗?

时间:2018-02-14 05:23:52

标签: java apache-poi fileoutputstream

在我的代码中,我使用的是Apache POI库类 - HSSFWorkbook

try {
    testBook.write(outputStream);
} catch (Exception e) {
    throw new ExportException(e);
}

正如您所看到的,我正在将FileOutputStream对象传递给write()方法。 我的问题是,是否需要在finally块中关闭此流对象或POI在内部执行此操作?

我已经看到POIFSFileSystem.writeFilesystem()方法是从HSSFWorkbook.write()方法和writeFilesystem()调用的 写入以下代码的方法,其中将流对象传递给writeBlocks()方法。但我不确定在运行时间 哪个类writer变量将引用的实例。所以我最后不知道该流对象发生了什么。

请帮忙。

iter = writers.iterator();
while (iter.hasNext())
{
    BlockWritable writer = ( BlockWritable ) iter.next();

    writer.writeBlocks(stream);
}

1 个答案:

答案 0 :(得分:1)

The Javadoc for the method没有提及关闭流,因此您有责任在必要时关闭它。请注意,事实上the implementation does not close the stream;从理论上讲,这可以促进诸如实现序列化形式之类的东西。

相关问题