开发者

Can't get the correct file path from JFileChooser

开发者 https://www.devze.com 2022-12-12 15:00 出处:网络
I\'ve created a JFileChooser which I use to locate a directory for a file to be saved to. saveChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

I've created a JFileChooser which I use to locate a directory for a file to be saved to.

saveChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
saveChooser.showSaveDialog(null);
String exportPath = saveChooser.getCurrentDirectory() + "\\exportedData.txt";
System.out.println(exportPath);

(I then use exportPath for my file writer)

When I choose C:\Users\'me'\Eclipse\workspace\'project'\files in the JFileChooser the output is C:\Users\'me'\Eclipse\workspace\'project'\exportedData.txt

I've tried saving to multiple locations. It seems like it always drops the last directory. However, when I select root C:\ it uses My Documents which is the default I suppose. I know this 开发者_StackOverflowprobably isn't the best way to save a file and opens up to a few bugs, sorry. (The file name and extension is set in code).


I would guess that you want saveChooser.getSelectedFile(), and that in this case the selected file is really a directory. Hope that's right, didn't test.

0

精彩评论

暂无评论...
验证码 换一张
取 消