简体   繁体   English

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

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

If I have the following code:如果我有以下代码:

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");
}

Once I'm outside of the try-with-resources block, am I guaranteed that the file located at some/file/path is fully written?一旦我在 try-with-resources 块之外,我是否可以保证位于some/file/path已完全写入? I looked into the PDFBox source code but I couldn't get a confirmation.我查看了 PDFBox 源代码,但无法得到确认。

If a pdf API contains an explicit save method, you can usually assume that it creates a complete pdf file representing the document state at the time you call that method.如果 pdf API 包含显式save方法,您通常可以假设它创建了一个完整的 pdf 文件,代表您调用该方法时的文档状态。

If a pdf API early on accepts some OutputStream to continously write to, though, it requires some signal when to finalize the output;但是,如果早期的 pdf API 接受一些OutputStream来连续写入,则它需要一些信号何时完成输出; this signal usually is an explicit close call or an implicit closing by try-with-resources .此信号通常是显式close调用或try-with-resources隐式关闭。

The pdfbox API is of the former type, so the save call already suffices. pdfbox API 是前一种类型,所以save调用已经足够了。 Nonetheless, it's a good idea to explicitly or implicitly close the document here, too, for proper resource handling.尽管如此,为了适当的资源处理,在此处显式或隐式关闭文档也是一个好主意。

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

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