8

I'd like to create a directory in the user's 'Documents' folder, but so far I've only found out how to get the user's home directory:

javax.swing.JFileChooser fr = new javax.swing.JFileChooser();
javax.swing.filechooser.FileSystemView fw = fr.getFileSystemView();
this.userDirectory = fw.getDefaultDirectory();

In Windows the above code returns the 'My Documents' directory, which is great, that's where the new documents are supposed to go. On OS X it only returns the home directory.

Adding 'Documents' to the returned path would cause problems with localization.

How can I do this?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
oldbeamer
  • 1,285
  • 2
  • 15
  • 24
  • The proper Objective-C method is discussed at http://stackoverflow.com/questions/272544/whats-the-best-way-to-find-the-users-documents-directory-on-an-iphone - how/if that can be done in Java, I don't know. – nobody Feb 20 '09 at 01:16

3 Answers3

9

System.getProperty("user.home")+File.separator+"Documents";

And don't worry about with localization, look:

macb:Documents laullon$ pwd
/Users/laullon/Documents

My OS X is in Spanish.

  • 2
    It turns out that OS X localization works by changing the name displayed to the user, not the actual filename. Reference below. http://developer.apple.com/documentation/MacOSX/Conceptual/BPInternational/Articles/LocalizingPathnames.html#//apple_ref/doc/uid/20002141-BBCFJBFB – oldbeamer Feb 20 '09 at 03:10
  • Makes life easy but I'm not sure I like it very much :) – Tony Edgecombe Jun 23 '09 at 13:02
  • The best and safest answer is to use Apples FileManager – Paul Taylor Aug 16 '12 at 08:45
9

You want to use Apple's eio.FileManager extension:

    static public String documentsDirectory()
            throws java.io.FileNotFoundException {
        // From CarbonCore/Folders.h
        final String kDocumentsDirectory = "docs";
        return com.apple.eio.FileManager.findFolder(
            com.apple.eio.FileManager.kUserDomain,
            com.apple.eio.FileManager.OSTypeToInt(kDocumentsDirectory)
        );
    }

Documentation

vasi
  • 1,026
  • 7
  • 6
  • 2
    Thanks, this just helped me - I needed it to find the user's music folder. The extension isn't a download; it's within the JRE bundled in Mac OS X. Therefore, you have to write this code reflectively if you have code that must compile on other platforms. Finally, a reference to all the folder types can be found at http://walteriankaye.com/as/foldertypes.html – Dan Gravell Sep 10 '10 at 12:27
  • Or you can download a stub of the extension, then create a wrapper class which only ever gets called if platform is osx, this avoids having to write reflection code. – Paul Taylor Aug 16 '12 at 08:43
-1

On mac (as far as I know all recent 10.x variants) the "My Documents" directory is located at (from root):
/Users/< username >/Documents
Thus it is in the "Documents" sub-directory within the home directory for the user.

Demi
  • 6,147
  • 7
  • 36
  • 38