2013-08-03 50 views
0

我已經看到有關此主題的另一個問題。 我試過,但它仍然不是爲我工作由PDFRenderer打開後無法刪除pdf

我的代碼:

File file = new File("C:\\Testing\\abc.pdf"); 
RandomAccessFile raf = new RandomAccessFile(file, "r"); 
ReadableByteChannel ch = Channels.newChannel(new FileInputStream(file)); 
FileChannel channel = raf.getChannel(); 
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
PDFFile pdffile = new PDFFile(buf); 

/* The rest code to convert pdf to image 
* according to number of pages in pdf given above, which will not be provided here 
*/ 

/* Closing file */ 
raf.close(); 
ch.close(); 
channel.close(); 
buf.clear(); 

我的代碼不能正常工作,它沒有關閉文件

我的程序後,我無法刪除文件它跑了,它說Java SE Binary Platform已經打開了這個文件。

如何關閉由PDFRenderer打開的文件?

+0

看看這裏http://www.jarvana.com/jarvana/view/org/xhtmlrenderer/core-renderer/R8/core-renderer-R8-sources.jar!/org/xhtmlrenderer/simple/PDFRenderer。 java?format = ok – Algorithmist

+2

關閉了'pdffile'? – gigadot

+0

我希望我可以關閉它,但試圖通過使用pdffile.close()來關閉它將導致「無法找到符號」 我讀過你的鏈接Algorithmist,謝謝但我沒有使用OutputStream,所以我沒有知道他們是否對我有用 –

回答

0

從第一行和第三行刪除額外的')'。

File file = new File("C:\\Testing\\abc.pdf")); //last ')' one 
ReadableByteChannel ch = Channels.newChannel(new FileInputStream(file)); //last ')' one 

希望它能幫助你。

+0

對不起,這是一個錯字,我粘貼後在代碼中不小心鍵入了額外的')' –

+0

錯誤似乎在未顯示的休息(或關閉fileChannel)。 PDFFile pdfFile = new PDFFile(new File(file)); PdfReader pdfReader = pdfFile.getPdfReader(); try { ... } finally { pdfReader.close(); } – Ketan

+0

我剛剛刪除了缺少的代碼,只發現問題依然存在。 我設法找出它可能是ByteBuffer的問題,但我不知道如何解決它 –