JFileChooser getCurrentDirectory返回错误的当前目录?

10
我正在使用JFileChooser浏览文件夹,但是当我选择文件夹时,它返回所选文件夹的上一级目录路径。 例如,我选择了“C:\ Test”,它会返回“C:\”。
这是我正在使用的代码。
            JFileChooser c = new JFileChooser();
            c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int rVal = c.showSaveDialog(c);
            if (rVal == JFileChooser.APPROVE_OPTION) {
                txtDirectory.setText("");
                CC_Test.MsgBox(c.getCurrentDirectory().toString());
                txtDirectory.setText(c.getCurrentDirectory().toString());
            }
            if (rVal == JFileChooser.CANCEL_OPTION) {
                txtDirectory.setText("");
            }
3个回答

16

你应该使用

c.getSelectedFile()

与其使用

c.getCurrentDirectory()

为了获取所选文件(在这种情况下是目录),必须使用该方法。否则,它将返回文件选择器面板中显示的目录(即父目录),而不是所选目录。


3

你需要使用JFileChooser.getSelectedFile()方法。这个方法返回的是File类对象,可以用于操作文件和目录。


3

要获取所选文件或目录,请使用:

c.getSelectedFile();

如果您使用

c.getCurrentDirectory();

返回值取决于操作系统。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接