我正在使用 JFileChooser 来创建保存功能。 但我想将文件保存为 .rtf 格式,该怎么做?
我现在的代码是:
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser saveFile = new JFileChooser();
int option = saveFile.showSaveDialog(null);
saveFile.setDialogTitle("Save the file...");
// saveFile.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf"));
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
writer.write(text);
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(null, "Failed to save the file");
}
}
}//End of method
}//End of inner class
所以我需要实现将文件保存到 .rtf 中的代码
最佳答案
假设(在没有 SSCCE 的情况下)JEditorPane
已设置为 text/rtf
并包含一些格式化文本,则序列化它的最简单方法是:
editorPane.write( writer );
参见JTextComponent.write(Writer)
了解详情。
关于java - 如何将文件保存为 .rtf 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14981115/