Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting My Documents path in Java

Tags:

java

windows

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.

like image 693
Em Ae Avatar asked Mar 13 '12 02:03

Em Ae


People also ask

What is the path for my documents?

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.

How do I access a shared drive in Java?

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.

What is getAbsolutePath in Java?

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.


2 Answers

That's easy, JFileChooser finds it for you

new JFileChooser().getFileSystemView().getDefaultDirectory().toString(); 

I hope this helps someone

like image 102
xchiltonx Avatar answered Sep 18 '22 11:09

xchiltonx


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

like image 35
IvanRF Avatar answered Sep 20 '22 11:09

IvanRF