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.
To view the full path of an individual file: Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document.
It is possible to mount shared folder "\\common\" on a local directory using SAMBA. E.g. you mount "\\common\" to "/mnt/common/" and then load file "/mnt/common/myfile. txt" from your java app.
The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object. For Example: if we create a file object using the path as “program.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With