无法删除文件,因为它是由于合并类而打开的(pdf)

时间:2017-01-02 15:31:38

标签: java

运行此代码时出现以下错误。我发现它是因为merger.mergesPagesFromPdfFiles()函数而发生的。我怎样才能防止这种情况发生?我尝试了几件事,但没有任何效果。

  

java.nio.file.FileSystemException:C:\ Users \ Ben \ Desktop \ Post \ Klanten \ 10817 - TEST TEST \ Factuur TEST TEST.pdf :(已翻译)该进程无权访问,因为该文件由另一个过程

Merger merger = new Merger();
merger.mergesPagesFromPdfFiles(filesToSend, getPathPostTS().toString() + "\\"
        + CustomFunctions.generatePersonalFileName("Documenten", client) + ".pdf");
//Runtime.getRuntime().gc();

//merger = null;
for (String file : filesToSend) {
    File fileEntry = new File(file);
    try {
        fileEntry.setWritable(true);
        java.nio.file.Files.deleteIfExists(fileEntry.toPath());
        //Another way of trying to delete it
        if (fileEntry.delete()) {
            System.out.println(fileEntry.getName() + " is deleted!");
        } else {
            System.out.println("Delete operation is failed.");
        }
    } catch (Exception e) {

        e.printStackTrace();

    }
}
System.out.println(folder.list());
if (folder.list().length > 0) {

    folder.delete();

} else {
    JOptionPane.showMessageDialog(null, "FOLDER IS NIET LEEG, KIJK NA");
}

这是创建冲突的合并类。

public Merger() {

}



public void mergesPagesFromPdfFiles(List<String> pdfFiles, String outputFileLocation) {
    try {
        Document document = new Document();
        FileOutputStream fos = new FileOutputStream(outputFileLocation);
        PdfCopy writer = new PdfCopy(document,fos );
        writer.setMergeFields();
        document.open();

        for (String pdf : pdfFiles) {
            int index = pdf.lastIndexOf("\\");
            String pdfName = pdf.substring(index + 1, pdf.indexOf(".pdf"));
            PdfReader reader = new PdfReader(pdf);
            // if(reader.getNumberOfPages() < mergePageNr){
            // System.err.println("'" + pdfName + "' has not enough pages to
            // merge. Skipped to next pdf!");
            // continue;
            // }
            writer.addDocument(reader);
        }
        System.out.println("Files merged!");
        document.close();
        writer.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

感谢您的帮助!

编辑:完成错误以及更改代码以关闭fos

  

文件已合并!       java.nio.file.FileSystemException:C:\ Users \ Ben \ Desktop \ Post \ Klanten \ 10817 - TEST TEST \ Factuur TEST TEST.pdf:Het proces heeft geen toegang tot het bestand omdat het door een ander proces wordt gebruikt。       at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)       at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)       at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)       at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)       at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)       at java.nio.file.Files.deleteIfExists(Unknown Source)       at generalClasses.Generator.sendAllFilesWithPost(Generator.java:243)       在factuurGen.FactuurGenerator。(FactuurGenerator.java:63)       在factuurGen.FactuurOptionPanel.generate(FactuurOptionPanel.java:31)       at view.MainView.generateAllDocuments(MainView.java:368)       at view.MainView.lambda $ 1(MainView.java:309)       在javax.swing.AbstractButton.fireActionPerformed(未知来源)       at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)       在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)       在javax.swing.DefaultButtonModel.setPressed(未知来源)       在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)       at java.awt.Component.processMouseEvent(Unknown Source)       在javax.swing.JComponent.processMouseEvent(未知来源)       at java.awt.Component.processEvent(Unknown Source)       at java.awt.Container.processEvent(Unknown Source)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 500(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       在java.awt.EventQueue $ 3.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 4.run(未知来源)       在java.awt.EventQueue $ 4.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       在java.awt.EventDispatchThread.run(未知来源)

0 个答案:

没有答案