我正在使用 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 的代码