【发布时间】:2020-09-27 09:59:33
【问题描述】:
我正在尝试创建一个 pdf 文件,然后使用 fileChooser 将其保存到设备 它可以保存但是当我去文件打开它时它没有打开 这是我的代码
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();
}
当我打开它时,这是显示文件已被其他用户打开或使用的错误
【问题讨论】:
-
你关闭
FileOutputStream了吗?我只能看到你flush()它...close()它在flush()之后。 -
@deHaar 是的,非常感谢它的工作,你可以把它放在一个独立的答案中,这样我就可以将它标记为已回答
-
我不知道我应该关闭 FileOutputStream 我以为我们只关闭文档
标签: java pdf save filechooser