简体   繁体   中英

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? I looked into the PDFBox source code but I couldn't get a confirmation.

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.

If a pdf API early on accepts some OutputStream to continously write to, though, it requires some signal when to finalize the output; this signal usually is an explicit close call or an implicit closing by try-with-resources .

The pdfbox API is of the former type, so the save call already suffices. Nonetheless, it's a good idea to explicitly or implicitly close the document here, too, for proper resource handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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