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

我想在用户的’Documents’文件夹中创建一个目录,但到目前为止我还没有找到如何获取用户的主目录:

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

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

将“文档”添加到返回的路径会导致本地化问题。

我怎样才能做到这一点?

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

不要担心本地化,看看:

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

我的OS X是西class牙语。

您想使用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) ); } 

文档

在Mac上(据我所知最近的10.x变种),“我的文档”目录位于(从root):
/ Users / <用户名> /文件
因此,它位于用户主目录内的“Documents”子目录中。