macos 如何在 OS X 中使用 Java 找到用户的“文档”文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/567874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the user's 'Documents' folder with Java in OS X?
提问by oldbeamer
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.
在 Windows 中,上面的代码返回“我的文档”目录,这很好,这就是新文档应该去的地方。在 OS X 上,它只返回主目录。
Adding 'Documents' to the returned path would cause problems with localization.
将“文档”添加到返回的路径会导致本地化问题。
How can I do this?
我怎样才能做到这一点?
回答by vasi
You want to use Apple's eio.FileManager extension:
你想使用 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)
);
}
回答by vasi
System.getProperty("user.home")+File.separator+"Documents";
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.
我的 OS X 是西班牙语的。
回答by Demi
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.
在 Mac 上(据我所知所有最近的 10.x 变体)“我的文档”目录位于(从根目录):
/Users/< username >/Documents
因此它位于用户的主目录。