保存一个文件,文件格式为PDF文件,使用Java。

huangapple go评论83阅读模式
英文:

saving a file fiormat pdf file with java

问题

我正在尝试创建一个PDF文件,然后使用文件选择器将其保存到设备上。
保存部分是有效的,但当我尝试打开文件时,它无法打开。
以下是我的代码:

FileChooser fc = new FileChooser();
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF文件", "*.pdf"));
fc.setTitle("保存为PDF");
fc.setInitialFileName("未命名.pdf");
Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();

File file = fc.showSaveDialog(stg);
if (file != null) {
    String str = file.getAbsolutePath();
    FileOutputStream fos = new FileOutputStream(str);
    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(str));
    document.open();
    document.add(new Paragraph("一个你好世界的PDF文档。"));
    document.close();
    writer.close();

    fos.flush();
}

当我尝试打开文件时,出现错误,显示文件已被其他用户打开或正在使用。

英文:

I am trying to create a pdf file then save it to device using fileChooser
It works the saving but when i go to the file to open it it doesn't open
here is my code

 FileChooser fc = new FileChooser();
        fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF File", "*.pfd"));
        fc.setTitle("Save to PDF"
        );
        fc.setInitialFileName("untitled.pdf");
        Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();

        File file = fc.showSaveDialog(stg);
        if (file != null) {
            String str = file.getAbsolutePath();
            FileOutputStream fos = new FileOutputStream(str);
            Document document = new Document();

            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(str));
            document.open();
            document.add(new Paragraph("A Hello World PDF document."));
            document.close();
            writer.close();

            fos.flush();

        }

when i open it this is the error showing it says the file is either opened or used by another user

答案1

得分: 0

你的代码没有调用close()方法关闭FileOutputStream,这可能会导致资源泄漏,并且文档可能无法正确访问,甚至可能损坏。

当你使用实现了AutoClosable接口的FileOutputStream时,有两个选择:

手动调用close()关闭FileOutputStream

FileChooser fc = new FileChooser();
fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF 文件", "*.pdf"));
fc.setTitle("保存为 PDF");
fc.setInitialFileName("未命名.pdf");
Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();

File file = fc.showSaveDialog(stg);
if (file != null) {
    String str = file.getAbsolutePath();
    FileOutputStream fos = new FileOutputStream(str);
    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(str));
    document.open();
    document.add(new Paragraph("一个你好世界的 PDF 文档。"));
    document.close();
    writer.close();

    fos.flush();
    /*
     * 唯一与你的代码不同的地方在于下面这一行
     */
    fos.close();
}

或者使用try与资源一起使用,详情请参阅这篇帖子

英文:

Your code does not close() the FileOutputStream which may cause a resource leak and the document is not properly accessible, maybe even corrupted.

When you work with a FileOutputStream that implements AutoClosable, you have two options:

close() the FileOutputStream manually:

FileChooser fc = new FileChooser();
    fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF File", "*.pfd"));
    fc.setTitle("Save to PDF");
    fc.setInitialFileName("untitled.pdf");
    Stage stg = (Stage) ((Node) event.getSource()).getScene().getWindow();

    File file = fc.showSaveDialog(stg);
    if (file != null) {
        String str = file.getAbsolutePath();
        FileOutputStream fos = new FileOutputStream(str);
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(str));
        document.open();
        document.add(new Paragraph("A Hello World PDF document."));
        document.close();
        writer.close();

        fos.flush();
        /*
         * ONLY DIFFERENCE TO YOUR CODE IS THE FOLLOWING LINE
         */
        fos.close();
    }
}

or use a try with resources read about that in this post, for example.

huangapple
  • 本文由 发表于 2020年9月27日 17:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64087174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定