如何在OS X中找到用户的'Documents'文件夹?

old*_*mer 7 java macos

我想在用户的'Documents'文件夹中创建一个目录,但到目前为止我只发现了如何获取用户的主目录:

javax.swing.JFileChooser fr = new javax.swing.JFileChooser();
javax.swing.filechooser.FileSystemView fw = fr.getFileSystemView();
this.userDirectory = fw.getDefaultDirectory();
Run Code Online (Sandbox Code Playgroud)

在Windows中,上面的代码返回"我的文档"目录,这是很好的,这是新文档应该去的地方.在OS X上,它只返回主目录.

将"文档"添加到返回的路径会导致本地化问题.

我怎样才能做到这一点?

小智 9

您想使用Apple的eio.FileManager扩展:

    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)
        );
    }
Run Code Online (Sandbox Code Playgroud)

文档

  • 谢谢,这只是帮助了我 - 我需要它来找到用户的音乐文件夹.扩展程序不是下载; 它位于Mac OS X中捆绑的JRE中.因此,如果您的代码必须在其他平台上编译,则必须反思性地编写此代码.最后,可以在http://walteriankaye.com/as/foldertypes.html找到对所有文件夹类型的引用. (2认同)

小智 8

System.getProperty( "的user.home")+文件分割符+ "文档";

不要担心本地化,看看:

macb:Documents laullon$ pwd
/Users/laullon/Documents
Run Code Online (Sandbox Code Playgroud)

我的OS X是西班牙语.

  • 事实证明,OS X 本地化的工作原理是更改向用户显示的名称,而不是实际的文件名。参考如下。http://developer.apple.com/documentation/MacOSX/Conceptual/BPInternational/Articles/LocalizingPathnames.html#//apple_ref/doc/uid/20002141-BBCFJBFB (2认同)