我有一个按钮,可以打开一个保存对话框窗口,其中设置了默认的扩展过滤器。当我保存文件时,它会在正确的文件夹中创建它,但它不会在最后添加扩展名。我将如何根据过滤器将扩展名添加到文件末尾?这是我的保存文件按钮代码:
btnNewButton = new JButton("Export File");
btnNewButton.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JFileChooser finder = new JFileChooser();
finder.setFileFilter(new FileNameExtensionFilter("Board Files", "boa"));
int returnVal = finder.showSaveDialog(null);
if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
java.io.File file = finder.getSelectedFile();
String file_name = file.toString();
JOptionPane.showMessageDialog(null, file_name);
WriteFile data = new WriteFile(file_name);
try {
data.writeToFile("Testing 1");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
});