gpt4 book ai didi

java - pdfBox 之后我无法删除文件

转载 作者:行者123 更新时间:2023-12-02 02:55:16 24 4
gpt4 key购买 nike

使用 PdfBox 后无法删除 pdf 文件。所有可关闭的变量都已关闭,但这没有帮助。当我使用 PdfBox 库启动应用程序时,我无法在任何地方删除 pdf 文件。

public void generateTxtFromPDF(String txtFile, String pdfFile) throws IOException {
COSDocument cosDoc = null;
PDDocument document = null;
PrintWriter pw = null;
try {
File f = new File(pdfFile);
String parsedText;
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
parser.parse();
cosDoc = parser.getDocument();
PDFTextStripper pdfStripper = new PDFTextStripper();
document = new PDDocument(cosDoc);
parsedText = pdfStripper.getText(document);
pw = new PrintWriter(txtFile);
pw.print(parsedText);
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (cosDoc != null) {
cosDoc.close();
}
}
}

也许,有人知道出了什么问题吗?

最佳答案

我修复了这个问题 - 更改 JDK 版本 (jdk1.8.0_181 => jdk1.8.0_221)。感谢您对我的问题感兴趣。并使用此代码:

    public void generateTxtFromPDF(String txtFile, String pdfFile) throws IOException {
File f = new File(pdfFile);
String parsedText;
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
parser.parse();
PDDocument document = null;
try (PrintWriter pw = new PrintWriter(txtFile);
COSDocument cosDoc = parser.getDocument()) {
document = new PDDocument(cosDoc);
PDFTextStripper pdfStripper = new PDFTextStripper();
parsedText = pdfStripper.getText(document);
pw.print(parsedText);
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
} finally {
document.close();
document = null;
}
}

关于java - pdfBox 之后我无法删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57081299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com