繁体   English   中英

将 PDDocument 与 try-with-resources 一起使用是否保证文件已完全写入?

[英]Does Using PDDocument with try-with-resources guarantee that a file is fully written?

如果我有以下代码:

try(PDDocument myDoc = new PDDocument()) {
  // add to myDoc in a giant loop so that the resulting file
  // would be quite large
  .
  .
  .
  myDoc.save("/some/file/path");
}

一旦我在 try-with-resources 块之外,我是否可以保证位于some/file/path已完全写入? 我查看了 PDFBox 源代码,但无法得到确认。

如果 pdf API 包含显式save方法,您通常可以假设它创建了一个完整的 pdf 文件,代表您调用该方法时的文档状态。

但是,如果早期的 pdf API 接受一些OutputStream来连续写入,则它需要一些信号何时完成输出; 此信号通常是显式close调用或try-with-resources隐式关闭。

pdfbox API 是前一种类型,所以save调用已经足够了。 尽管如此,为了适当的资源处理,在此处显式或隐式关闭文档也是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM