0

I need to delete file right before exiting method:

String holdInFdfFile = "myPDF1";
this.holdModifiedPDFDir = "C:\\pdfs\\Jobs\\";
String tempString = this.holdModifiedPDFDir + "\\" + holdInFdfFile;

//Create Flattened PDF
PdfReader PDFreaderFlattened = new PdfReader(this.holdOriginalPDFDir + holdInPdfFile);
FileOutputStream foutFlattened = new FileOutputStream(this.holdModifiedPDFDir + holdInFdfFile + "_FLAT.pdf");
PdfStamper stampFlattened = new PdfStamper(PDFreaderFlattened, foutFlattened);
FdfReader holdFDFreaderFlattened = new FdfReader(tempString);
stampFlattened.setFreeTextFlattening(true);
stampFlattened.setFullCompression();
stampFlattened.setFormFlattening(true);
stampFlattened.setFullCompression();
AcroFields formFlattened = stampFlattened.getAcroFields();
formFlattened.setFields(holdFDFreaderFlattened);

stampFlattened.close();
foutFlattened.close();

I need to delete foutFlattened file.

I also noticed, even thought I close() it, it doesn't allow me to delete the file unless I kill the process.

I am using java 1.80_92

Angelina
  • 2,175
  • 10
  • 42
  • 82

1 Answers1

1

There is an issue that seems to come up from time to time with Java 6. I haven't seen it in Java 7, but if you close it out and it still seems like something is hanging onto the file, you may have to call for a gc in order to get the jvm to fully release the file so that you can delete it. Horrid practice, but that was the only thing that worked when I've run into this in the past.

John
  • 376
  • 1
  • 7
  • that didn't fix closing of the file. I also need to delete this file, just before I exit method. – Angelina May 04 '16 at 20:01
  • This is Java 6 bug imho and the reason why close() method was added to URLClassloader in Java 7. – Andrei_N May 04 '16 at 20:04
  • Also, I believe that PDFStamper does not close the underlying PDFReader, so you still have handles to the file open. You will need to close out each that has a handle to the file. Looking, I see that someone may have updated the code at some point to close the underlying reader, but that fix may or may not be in the version you are using (or released yet?). Anyways, I would start there and try to explicitly call each close yourself on each I/O class you are using. – John Jun 14 '16 at 17:35