0

I am opening a PDF File that I have created with iText. Without opening it in PDF Renderer and just opening it normally and closing it I can delete and overwrite to the file. However once I use PDF Renderer to open it then I close it. I am unable to delete the PDF or replacing it.

ByteBuffer buf;

...

randomAccessFile = new RandomAccessFile(new File(file), "r");
fileChannel = randomAccessFile.getChannel();
buf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pdfFile = new PDFFile(buf);

Code here then gets the image and puts it into a panel.

Then I close the above items.

It is still opened somewhere but I can not figure out where, any help is appreciated.

2 Answers2

1

The error seems to be in the unshown rest (or closing the fileChannel).

PDFFile pdfFile = new PDFFile(new File(file));
PdfReader pdfReader = pdfFile.getPdfReader();
try {
    ...
} finally {
    pdfReader.close();
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Hey thanks for the reply. However I do not use the PdfReader class. I get the PDFPage and from that page a produce an image and put it onto a JLabel. I then finally close all the other bits but the file is still open for some reason. – FlavouredDavid Dec 05 '12 at 19:31
  • 2
    Closing randomAccessFile and fileChannel too, of course. Then you can only comment-out parts of the code, like PDFPage, and then look what was held open. – Joop Eggen Dec 05 '12 at 20:39
  • Yea I have closed them. I found that it is to do with the ByteBuffer. When I clear it, it still has the file attached to it. Once I made the ByteBuffer null after clearing it, it worked correctly. Thanks for the advice. – FlavouredDavid Dec 05 '12 at 21:25
0

Do you close the file? If you still have an Open file, Java cannot delete it.

mark stephens
  • 3,205
  • 16
  • 19