Reputation: 24685
There is a JFileChooser
dialog where a user browse to a location which contains its config file. I want to get that location as the working directory however, System.getProperty("user.dir")
seems to point to the location where the application starts. How can I fix that?
Assume
D:\netbean\projects\test
that is where the application start. Then the user clicks on a button and browse to
D:\configs
The code look like
File selectedFile = fc.getSelectedFile();
myTextArea.setText("Working directory is " + System.getProperty("user.dir") + "\n" );
That points to netbeans folder which is wrong in my case.
Upvotes: 0
Views: 72
Reputation: 131324
System.getProperty("user.dir")
is a system property defined at runtime that is
the directory where the JVM was run from.
It has no relation with the directory that contains the file chosen in a JFileChooser
.
You can use the getParentFile()
method of File
to retrieve the folder that contains the file that was chosen by the user:
File selectedFile = fc.getSelectedFile();
myTextArea.setText("Parent directory is " + selectedFile.getParentFile() + "\n" );
Upvotes: 3