如何在Java中使用JFileChooser保存文件?[已关闭]
2022-09-02 01:13:35
我有以下代码。它保存文件,但内容为空。这是怎么回事?
public void saveMap() {
String sb = "TEST CONTENT";
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("/home/me/Documents"));
int retrival = chooser.showSaveDialog(null);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()+".txt");
fw.write(sb.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}