从 java 阅读时关闭 PDF 文件
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 的错误。
使用PDDocument
or the COSDocument
实例的close()
方法(两种情况下都会调用COSDocument
对象的close
方法)
您正在使用过时的文件打开方式。这是正确的做法(省略异常处理):
File pdfFile = new File("ffile.pdf");
PDDocument pdDoc = PDDocument.load(pdfFile);
PDFTextStripper pdfStripper = new PDFTextStripper();
//....
pdDoc.close();
我正在阅读 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 的错误。
使用PDDocument
or the COSDocument
实例的close()
方法(两种情况下都会调用COSDocument
对象的close
方法)
您正在使用过时的文件打开方式。这是正确的做法(省略异常处理):
File pdfFile = new File("ffile.pdf");
PDDocument pdDoc = PDDocument.load(pdfFile);
PDFTextStripper pdfStripper = new PDFTextStripper();
//....
pdDoc.close();