我创建了一个 JFileChooser,我用它来定位要保存到的文件的目录。
saveChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
saveChooser.showSaveDialog(null);
String exportPath = saveChooser.getCurrentDirectory() + "\\exportedData.txt";
System.out.println(exportPath);
(然后我使用 exportPath 作为我的文件编写器)
当我在 JFileChooser 中选择 C:\Users\'me'\Eclipse\workspace\'project'\files 时,输出为 C:\Users\'me'\Eclipse\workspace\'project'\exportedData.txt
我试过保存到多个位置。似乎它总是删除最后一个目录。但是,当我选择根 C:\ 时,它使用我认为的默认值 My Documents。我知道这可能不是保存文件的最佳方式,并且会带来一些错误,抱歉。(文件名和扩展名在代码中设置)。