I need to find my documents path using Java. The following code doesn't give me "accurate" loation
System.getProperty("user.home");
What should be the other way around?
P.S: I don't want to use the JFileChooser Dirty trick.
I need to find my documents path using Java. The following code doesn't give me "accurate" loation
System.getProperty("user.home");
What should be the other way around?
P.S: I don't want to use the JFileChooser Dirty trick.
That's easy, JFileChooser
finds it for you
new JFileChooser().getFileSystemView().getDefaultDirectory().toString();
I hope this helps someone
Since the most upvoted answer from @xchiltonx uses JFileChooser
I would like to add that, regarding performance, this is faster than using JFileChooser
:
FileSystemView.getFileSystemView().getDefaultDirectory().getPath()
In my PC, JFileChooser
aproach needed 300ms, and calling FileSystemView
directly needed less than 100ms.
Note: The question is a possible duplicate of How to find “My Documents” folder in Java
You can get it using a registry query, no need for JNA or admin rights for that.
Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell
Folders\" /v personal");
Obviously this will fail on anything other than Windows, and I am not certain whether this works for Windows XP.
EDIT: Put this in a working sequence of code:
String myDocuments = null;
try {
Process p = Runtime.getRuntime().exec("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal");
p.waitFor();
InputStream in = p.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);
in.close();
myDocuments = new String(b);
myDocuments = myDocuments.split("\\s\\s+")[4];
} catch(Throwable t) {
t.printStackTrace();
}
System.out.println(myDocuments);
Note this will lock the process until "reg query" is done, which might cause trouble dependeing on what you are doing.
Note in 2020: The ShellFolder class seems to be missing on Linux (tested with openjdk8), so IvanRF's answer is likely better.
The best way I've found is to use AWTs:
ShellFolder.get("fileChooserDefaultFolder");
I have redirected my Documents folder to the D: drive, and it successfully fetches this directory. It also does so in about 40 ms (on my machine). Using FileSystemView
takes about 48 ms, and new JFileChooser()
about 250 ms.
All three of these methods actually use ShellFolder
under the hood, and the difference with FileSystemView
is negligible, but calling it directly avoids the overhead of the other two.
Note: You can also cast this directly to File
instead of implicitly getting the toString()
method of it, which can help you further:
File documents = (File) ShellFolder.get("fileChooserDefaultFolder");
Using JNA you would do this:
String myDocsPath = Shell32Util.getFolderPath(ShlObj.CSIDL_PERSONAL);
JNA extracts a DLL on-the-fly and then uses JNI with this DLL to make Windows API calls. It hides all the JNI details from you though. Using JNA is as easy as using any other java library JAR.
"user.home"
returns the home directory of the user, not the "My Documents" folder.
On Windows, it would be "C:\Users\Username\" for Vista or 7, or "C:\Documents and Settings\Username" for XP
What you want is:
System.out.println(System.getProperty("user.home") + File.separatorChar + "My Documents");
this is what eclipse does to get the user document folder
System.getProperty("user.dir") //$NON-NLS-1$
+ File.separator + "workspace")
Hope it's helpfull!
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user") + (File.separatorChar + "My Documents")));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());