tags:

views:

296

answers:

4

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 realised 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 ?

A: 

Take a look at http://technet.microsoft.com/en-us/library/cc749369%28WS.10%29.aspx

Scavenger
Does not work for java
AB Kolan
Wouldn't "%CSIDL_PERSONAL%\doc.txt" open the file doc.txt in the My Documents folder, even in Java?
Scavenger
+3  A: 

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
+2  A: 

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
A: 

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