如何使用 JFileChooser 在 java 中获取选定的文件夹名称?

How to Get selected folder name in java using JFileChooser?

我想 select select 编辑的文件夹。

 JFileChooser targetDir = new JFileChooser();
            targetDir.setDialogTitle("Choose Target Directory.");
            targetDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if(targetDir.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)   
            {
                System.out.println(targetDir.getCurrentDirectory());
                main_mw = new MainWindow("XYZ Copier");
            main_mw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                        } else {
                System.exit(0);
            }
        } else {
        }

它给出输出“/home/rahul/Downloads/mc” 但我需要“/home/rahul/Downloads/mc/lib”。如果我进入 lib.

,它会给出相同的结果

截图:

JFileChooser#getSelectedFile 将 return 选定的 file/directory

getCurrentDirctory returns 当前显示在选择器中的目录