[英]Close a PDF file when read it from java
我正在從pdf文件中讀取一些文本
try {
File pdfFile = new File("ffile.pdf");
PDFParser parser = new PDFParser(new FileInputStream(pdfFile));
parser.parse();
COSDocument cosDoc = parser.getDocument();
PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument pdDoc = new PDDocument(cosDoc);
//do sth
} catch (Exception e) {
System.err.println("An exception occured in parsing the PDF Document."
+ e.getMessage());
}
有時我會收到此錯誤:
WARNING [Finalizer] org.apache.pdfbox.cos.COSDocument.finalize Warning: You did not close a PDF Document
我在這里讀到類似的問題,說我必須關閉打開的文件。 所以,我加了
finally{
pdfFile.close(); //<-
}
但是Netbeans的close()
錯誤標記為找不到符號。 那我要關閉什么? 我也嘗試了parser.close()
但是這一行也被Netbeans標記為錯誤。
您正在使用一種過時的方式打開文件。 這是正確的方法(省略了異常處理):
File pdfFile = new File("ffile.pdf");
PDDocument pdDoc = PDDocument.load(pdfFile);
PDFTextStripper pdfStripper = new PDFTextStripper();
//....
pdDoc.close();
使用PDDocument
或COSDocument
實例的close()
方法(在兩種情況下都調用COSDocument
對象的close
方法)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.