在 Java 中获取我的文档路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9677692/
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
Getting My Documents path in Java
提问by Em Ae
I need to find my documents path using Java. The following code doesn't give me "accurate" loation
我需要使用 Java 找到我的文档路径。以下代码没有给我“准确”的定位
System.getProperty("user.home");
System.getProperty("user.home");
What should be the other way around?
反过来应该是什么?
P.S: I don't want to use the JFileChooser Dirty trick.
PS:我不想使用 JFileChooser Dirty 技巧。
采纳答案by pdinklag
You can get it using a registry query, no need for JNA or admin rights for that.
您可以使用注册表查询获取它,不需要 JNA 或管理员权限。
Runtime.getRuntime().exec("reg query \"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders\" /v personal");
Obviously this will fail on anything other than Windows, and I am not certain whether this works for Windows XP.
显然这在 Windows 以外的任何东西上都会失败,我不确定这是否适用于 Windows XP。
EDIT: Put this in a working sequence of code:
编辑:把它放在代码的工作序列中:
String myDocuments = null;
try {
Process p = Runtime.getRuntime().exec("reg query \"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\" /v personal");
p.waitFor();
InputStream in = p.getInputStream();
byte[] b = new byte[in.available()];
in.read(b);
in.close();
myDocuments = new String(b);
myDocuments = myDocuments.split("\s\s+")[4];
} catch(Throwable t) {
t.printStackTrace();
}
System.out.println(myDocuments);
Note this will lock the process until "reg query" is done, which might cause trouble dependeing on what you are doing.
请注意,这将锁定进程,直到“reg 查询”完成,这可能会导致麻烦,具体取决于您在做什么。
回答by nbarraille
"user.home"
returns the home directory of the user, not the "My Documents" folder.
On Windows, it would be "C:\Users\Username\" for Vista or 7, or "C:\Documents and Settings\Username" for XP
"user.home"
返回用户的主目录,而不是“我的文档”文件夹。在 Windows 上,Vista 或 7 为“C:\Users\Username\”,XP 为“C:\Documents and Settings\Username”
What you want is:
你想要的是:
System.out.println(System.getProperty("user.home") + File.separatorChar + "My Documents");
回答by xchiltonx
That's easy, JFileChooser
finds it for you
很简单,JFileChooser
帮你找到
new JFileChooser().getFileSystemView().getDefaultDirectory().toString();
I hope this helps someone
我希望这可以帮助别人
回答by TuanAnh207
this is what eclipse does to get the user document folder
这就是 eclipse 为获取用户文档文件夹所做的工作
System.getProperty("user.dir") //$NON-NLS-1$
+ File.separator + "workspace")
Hope it's helpfull!
希望它有帮助!
回答by Sarel Botha
Using JNA you would do this:
使用 JNA 你会这样做:
String myDocsPath = Shell32Util.getFolderPath(ShlObj.CSIDL_PERSONAL);
JNA extracts a DLL on-the-fly and then uses JNI with this DLL to make Windows API calls. It hides all the JNI details from you though. Using JNA is as easy as using any other java library JAR.
JNA 即时提取一个 DLL,然后使用 JNI 和这个 DLL 进行 Windows API 调用。但是,它对您隐藏了所有 JNI 详细信息。使用 JNA 就像使用任何其他 Java 库 JAR 一样简单。
回答by IvanRF
Since the most upvoted answer from @xchiltonxuses JFileChooser
I would like to add that, regarding performance, this is fasterthan using JFileChooser
:
由于来自@xchiltonx的最受好评的答案使用JFileChooser
我想补充一点,关于性能,这比使用更快JFileChooser
:
FileSystemView.getFileSystemView().getDefaultDirectory().getPath()
In my PC, JFileChooser
aproach needed 300ms, and calling FileSystemView
directly needed less than 100ms.
在我的PC中,JFileChooser
aproach需要300ms,FileSystemView
直接调用需要不到100ms。
Note: The question is a possible duplicate of How to find “My Documents” folder in Java
注意:该问题可能与How to find “My Documents” folder in Java重复
回答by Jose Balicag
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user") + (File.separatorChar + "My Documents")));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
回答by Magnus Bull
The best way I've found is to use AWTs:
我发现的最好方法是使用 AWT:
ShellFolder.get("fileChooserDefaultFolder");
I have redirected my Documents folder to the D: drive, and it successfully fetches this directory. It also does so in about 40 ms (on my machine). Using FileSystemView
takes about 48 ms, and new JFileChooser()
about 250 ms.
我已将我的 Documents 文件夹重定向到 D: 驱动器,并且它成功获取了该目录。它也在大约 40 毫秒内完成(在我的机器上)。使用FileSystemView
大约需要 48 毫秒,new JFileChooser()
大约 250 毫秒。
All three of these methods actually use ShellFolder
under the hood, and the difference with FileSystemView
is negligible, but calling it directly avoids the overhead of the other two.
这三个方法实际上都是ShellFolder
在后台使用的,和 with 的区别FileSystemView
可以忽略不计,但是直接调用就避免了另外两个的开销。
Note:You can also cast this directly to File
instead of implicitly getting the toString()
method of it, which can help you further:
注意:您也可以将 this 直接转换为File
而不是隐式获取toString()
它的方法,这可以进一步帮助您:
File documents = (File) ShellFolder.get("fileChooserDefaultFolder");