Java在Apache POI上进行搜索和替换时如何避免覆盖模板文件

yev*_*yev 3 java apache-poi xwpf

我正在使用 Apache POI 3.13 并试图从给定的模板文件中搜索和替换文本,然后保存新生成的 .docx。这是我的代码:

public static void main(String[] args) throws InvalidFormatException, IOException {
    String filePath = "Sample.docx";
    File outputfile = new File("SampleProcessed.docx");

    XWPFDocument doc = new XWPFDocument(OPCPackage.open(filePath));

    for (XWPFParagraph p : doc.getParagraphs()) {
        List<XWPFRun> runs = p.getRuns();
        if (runs != null) {
            for (XWPFRun r : runs) {
                String text = r.getText(0);
                if (text != null && text.contains("$VAR")) {
                    text = text.replace("$VAR", "JohnDoe");
                    r.setText(text, 0);
                }
            }
        }
    }

    doc.write(new FileOutputStream(outputfile));
    doc.close();
    System.out.println("Done");
    Desktop.getDesktop().open(outputfile);
}
Run Code Online (Sandbox Code Playgroud)

这看起来很简单,但是当我运行这段代码时,文档“Sample.docx”也被替换了。最后,我有两个内容相同的文档。

这是 POI 的正常行为吗?我认为打开文档只会将其加载到内存中,然后执行 'doc.write(OutputStream);' 将其刷新到磁盘。

我尝试写入相同的“filePath”,但正如预期的那样,它抛出异常,因为我正在尝试写入当前打开的文件。

唯一有效的是当我首先复制模板文件并使用该副本时。但是现在,我有 3 个文件,第一个是原始模板“Sample.docx”,其余 2 个具有相同的内容(SampleProcessed.docx 和 SampleProcessedOut.docx)。

它有效,但它非常浪费。有什么办法吗?我做错了什么,也许我打开word文档有误?

Axe*_*ter 5

由于您正在使用

XWPFDocument doc = new XWPFDocument(OPCPackage.open(filePath));
Run Code Online (Sandbox Code Playgroud)

要创建XWPFDocument, aOPCPackagefilePathinREAD_WRITE模式打开。如果这将被关闭,它也将被保存。请参阅https://poi.apache.org/apidocs/org/apache/poi/openxml4j/opc/OPCPackage.html#close%28%29

OPCPackage将被关闭,而XWPFDocument将被关闭。

但你为什么要这样做?为什么不

XWPFDocument doc = new XWPFDocument(new FileInputStream(filePath));
Run Code Online (Sandbox Code Playgroud)

?

有了这个,XWPFDocument将仅在内存中创建OPCPackage一个与文件无关的新文件。