如何在Java中获取本地应用程序数据文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1198911/
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 to get local application data folder in Java?
提问by smf68
Possible Duplicate:
What is the cross-platform way of obtaining the path to the local application data directory?
可能的重复:
获取本地应用程序数据目录路径的跨平台方式是什么?
I'm looking for a way to get the location of the local application data folder, which is a special Windows folder, in Java. Unfortunately, the following only works for English versions of Windows XP with default settings:
我正在寻找一种方法来获取本地应用程序数据文件夹的位置,这是一个特殊的 Windows 文件夹,在 Java 中。不幸的是,以下仅适用于具有默认设置的英文版 Windows XP:
System.getProperty("user.home") + "\Local Settings\Application Data"
What I'd like to have is something like this in .NET:
我想要在 .NET 中是这样的:
System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Is there a way to do it without having to call SHGetSpecialFolderLocationof the Windows Shell API?
有没有办法在不必调用Windows Shell API 的SHGetSpecialFolderLocation的情况下做到这一点?
采纳答案by Frederik
Reading the "Shell Folders" registry key is deprecated starting from Windows 95. The registry key contains a note saying "!Do not use this registry key. Use the SHGetFolderPath or SHGetKnownFolderPath instead." I had to find this out the hard way on a Vista system where all the keys were missing except for the warning note.
从 Windows 95 开始,不推荐读取“Shell Folders”注册表项。注册表项包含一条说明“!不要使用此注册表项。改用 SHGetFolderPath 或 SHGetKnownFolderPath”。我必须在 Vista 系统上艰难地找到这一点,在该系统中,除了警告说明外,所有键都丢失了。
This related stackoverflow answersolves this problem on Windows using JNA, which is the solution I'm currently using.
这个相关的 stackoverflow 答案使用 JNA 在 Windows 上解决了这个问题,这是我目前使用的解决方案。
回答by Ryan Fernandes
System.getenv("APPDATA")
(there seems to be no env variable for the "Local Settings" folder, but this will give you the 'Application Data' folder)
(“本地设置”文件夹似乎没有环境变量,但这将为您提供“应用程序数据”文件夹)
回答by Gregor
You could read the path from the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\*
where *
is one of those Keys:
您可以从注册表中读取路径:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\*
其中*
一个键在哪里:
- Local AppData (
C:\Documents and Settings\USER\Local Settings\Application Data
) - Local Settings (
C:\Documents and Settings\USER\Local Settings
) - AppData (
C:\Documents and Settings\USER\Application Data
)
- 本地应用数据 (
C:\Documents and Settings\USER\Local Settings\Application Data
) - 本地设置 (
C:\Documents and Settings\USER\Local Settings
) - 应用数据 (
C:\Documents and Settings\USER\Application Data
)
Note: Those example paths are from an english Windows XP installation
注意:这些示例路径来自英文 Windows XP 安装
回答by McDowell
It would be possible to spawn a process to query the key and then parse the output:
可以生成一个进程来查询密钥,然后解析输出:
REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Local AppData"
Honestly, though, I'd be more inclined to use JNA or JNI.
不过,老实说,我更倾向于使用 JNA 或 JNI。
回答by Enrico Scantamburlo
I resolved in this way
我是这样解决的
private static File getAppData(){
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/C echo %APPDATA%"});
BufferedReader br = null;
try {
Process start = builder.start();
br = new BufferedReader(new InputStreamReader(start.getInputStream()));
String path = br.readLine();
// TODO HACK do not know why but I get an extra '"' at the end
if(path.endsWith("\"")){
path = path.substring(0, path.length()-1);
}
return new File(path.trim());
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, "Cannot get Application Data Folder", ex);
} finally {
if(br != null){
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
回答by jianinz
I would like to use the following two ways:
我想使用以下两种方式:
String dataFolder = System.getenv("APPDATA");
String dataFolder = System.getProperty("user.home") + "\Local Settings\ApplicationData";
回答by Charles Godwin
what about the following
下面呢
String dataFolder = System.getenv("LOCALAPPDATA");
I have a situation where this is NOT under "user.home"
我有一种情况,这不在“user.home”下