Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java file.getPath() returns language modified path

Tags:

java

I need to use

fileChooser.getSelectedFile()

method however it always returns language modified path because some directories are translated in osX. For example folder "/Downloads" is translated to my system language "/Stiahnuté" but real path is "/Downloads"

return:

/Users/John/Stiahnuté

expectation

/Users/John/Downloads

If I select some sub-directory then fileChooser.getSelectedFile() returns right path again. It looks that always only last directory in path is translated

/Users/John/Downloads/subDirectory

Code:

saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileFilter(new FolderFilter());
                fileChooser
                        .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    System.out.println("save path: "
                            + selectedFile.getPath());
                    doSomething(selectedFile);
                }
            }
       });

UPDATE:

I made little workaround but it is not perfect solution. However it works for me.

JFileChooser fileChooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "Directories", "dir");
                fileChooser.setFileFilter(filter);
                if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    File newDir = new File(selectedFile.getPath());
                    if (!newDir.exists()) {
                        newDir.mkdir();
                    }
                    doSomething();
                }
like image 550
Matwosk Avatar asked May 02 '16 23:05

Matwosk


People also ask

What does getPath return?

Simply put, getPath() returns the String representation of the file's abstract pathname. This is essentially the pathname passed to the File constructor. So, if the File object was created using a relative path, the returned value from getPath() method would also be a relative path.

What is the difference between getPath and getAbsolutePath in Java?

getPath(): The getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object. getAbsolutePath(): The getAbsolutePath() returns a path object representing the absolute path of given path.

What does getAbsolutePath return?

getAbsolutePath() method returns the absolute pathname string of this abstract pathname.


Video Answer


1 Answers

I can reproduce the problem on Mac OS X 10.11.4 with Java 1.8.0_66. For me this looks like a bug (or at least unexpected behavior) in the implementation of JFileChooser. You may open a bug report for the issue.

With the help of an answer explaining to use FileDialog to get a operating system native file chooser and another answer about using it to select directories I found the following workaround:

final Frame parent = …; // can be null

System.setProperty("apple.awt.fileDialogForDirectories", "true");
final FileDialog fileDialog = new FileDialog(parent);
fileDialog.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");

final File selectedDirectory = new File(fileDialog.getDirectory(), fileDialog.getFile());
System.out.println(selectedDirectory);
System.out.println(selectedDirectory.exists());

Note that using "apple.awt.fileDialogForDirectories" is, of course, platform specific and will not work on other operating systems.

like image 55
siegi Avatar answered Oct 07 '22 13:10

siegi