17

I'm willing to save a file in the user's "My Documents" folder.

I tried getting the location like this :

System.getenv("USERPROFILE") + "\\My Documents\\"

Then, I realized this wouldn't work in a system where the language is set to another language, french for example.

Is there another way of getting the "My Documents" folder efficiently?

IvanRF
  • 7,115
  • 5
  • 47
  • 71
Attilah
  • 17,632
  • 38
  • 139
  • 202

5 Answers5

21

Regarding performance, this is faster than using JFileChooser:

FileSystemView.getFileSystemView().getDefaultDirectory().getPath()

In my PC, JFileChooser approach needed 300ms, and calling FileSystemView directly needed less than 100ms.

IvanRF
  • 7,115
  • 5
  • 47
  • 71
17

If you don't mind depending on Swing you can apparently use this trick:

import javax.swing.JFileChooser;
javax.swing.filechooser.FileSystemView;

public class GetMyDocuments {
  public static void main(String args[]) {
     JFileChooser fr = new JFileChooser();
     FileSystemView fw = fr.getFileSystemView();
     System.out.println(fw.getDefaultDirectory());
  }
}

(source: http://www.rgagnon.com/javadetails/java-0572.html)

Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
  • 2
    For Mac OS X, the result of using this is one directory too high up actually. – Hakanai Jul 22 '14 at 04:43
  • This refers me to a folder that doesn't exist... c:\Users\\Documents. The documents folder on my system is called My Documents. – JavaLatte Mar 09 '16 at 16:27
  • @JavaLatte Are you sure that's the actual name? Explorer will show it as "My Documents" even if the directory is named "Documents", if you click the path bar you can see the real path. – Sean Van Gorder May 04 '17 at 15:22
  • 1
    @SeanVanGorder, you are right: if I open a command window and go to that directory, it is called "Documents", but in Explorer it is appears as "My Documents". – JavaLatte May 04 '17 at 17:46
7

There's winfoldersjava JNI extension for accessing special folder names in Java.

The winfoldersjava page also describes another method using Swing:

Also, if you need only the "My documents" path and don't mind touching Swing you may not need WinFoldersJava. You can use javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory() instead.

laalto
  • 150,114
  • 66
  • 286
  • 303
1

The location for that directory can be changed by each user, so it's not just dependent on the locale.
To find where that folder is, you have to look into the registry. The Windows command to do so is either:

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User
Shell Folders" /v personal

or

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell 
Folders" /v personal

not sure which location actually, but it's one of them.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • these registry entries are not created until one of other programs called Shell APIs – Sheng Jiang 蒋晟 Jun 30 '12 at 14:54
  • Also, running a subprocess just to pick up a registry value is pretty overblown. So many failure modes there, and you won't get proper error handling if e.g. the registry key happens to be absent, inaccessible, or the user's company has installed software that blocks `reg` from running (exactly my work scenario). – toolforger Feb 24 '21 at 18:41
-1

Take a look at http://technet.microsoft.com/en-us/library/cc749369(WS.10).aspx

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • 1
    Wouldn't "%CSIDL_PERSONAL%\doc.txt" open the file doc.txt in the My Documents folder, even in Java? – Wander Nauta Oct 01 '09 at 12:15
  • It probably would, but your program would no longer be platform independent. It seems better to use the Swing FileSystemView trick as suggested above, as that works cross-platform and also means you follow the same defaults as other Java apps do (always nice to be consistent). – Stijn de Witt Mar 21 '11 at 14:55