在 linux 和 macintosh 上查找“应用程序数据”的目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6561172/
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
Find directory for "application data" on linux and macintosh
提问by CodeBunny
I have a game engine based on LWJGL, and to run it I need to place the required native libraries onto the user's computer. On Windows, I do so by finding the Application Data directory via:
我有一个基于 LWJGL 的游戏引擎,要运行它,我需要将所需的本机库放置到用户的计算机上。在 Windows 上,我通过以下方式找到应用程序数据目录:
System.getenv("APPDATA");
and everything works easily and nicely. I create a File object, call mkDir if necessary, and write the files if they are not already on the machine.
一切都可以轻松而顺利地进行。我创建了一个 File 对象,必要时调用 mkDir,如果文件不在机器上,则写入文件。
(Note: the created directory should not be a temporary file, as I would like to save the extracted files for future runs. Additionally, creating this directory gives a simple and easy-to-use folder for saved games and other such data.)
(注意:创建的目录不应该是临时文件,因为我想保存提取的文件以备将来运行。此外,创建此目录为保存的游戏和其他此类数据提供了一个简单易用的文件夹。)
However, I'd like to do something similar if the computer is Macintosh or Linux, but I'm not as familiar with how to do so with those two systems, and I'm not really in a good testing position. My current method to find the target directory is this:
但是,如果计算机是 Macintosh 或 Linux,我想做类似的事情,但我不太熟悉如何使用这两个系统进行操作,而且我的测试位置也不是很好。我当前查找目标目录的方法是:
private static String defaultDirectory()
{
String OS = System.getProperty("os.name").toUpperCase();
if (OS.contains("WIN"))
return System.getenv("APPDATA");
else if (OS.contains("MAC"))
return System.getProperty("user.home") + "/Library/Application "
+ "Support";
else if (OS.contains("NUX"))
return System.getProperty("user.home");
return System.getProperty("user.dir");
}
So, is this the right way to do this? I'm trying to reach the Application Support on Mac (I've learned this is the equivalent of the AppData folder on windows) and I'm trying to use a similar folder on Linux, but I'm not sure if "user.home" finds the correct one.
那么,这是正确的方法吗?我正在尝试访问 Mac 上的应用程序支持(我了解到这相当于 Windows 上的 AppData 文件夹)并且我正在尝试在 Linux 上使用类似的文件夹,但我不确定“user.家”找到正确的。
采纳答案by Denis Tulskiy
this should work. just one thing: on linux it is preferred to keep settings in a hidden folder in user directory. So for linux, either put your folder under $HOME/.config, or start the name with the .
to make it hidden.
这应该有效。只是一件事:在 linux 上,最好将设置保存在用户目录的隐藏文件夹中。因此,对于 linux,请将您的文件夹放在 $HOME/.config 下,或者以 开头.
以使其隐藏。
回答by Fabian Zeindl
As already stated on Linux you should/could put your data in a .directory in the home.
正如在 Linux 上已经说过的,您应该/可以将您的数据放在家中的 .directory 中。
I have however a tip for you: "APPDATA" unfortunately doesn't always find the correct directory across several WindowsVersions. AFAIK the only know way i know how to do this correctly is calling a msdn-function called SHGetFolderPath
.
然而,我有一个提示:不幸的是,“APPDATA”并不总能在多个 Windows 版本中找到正确的目录。AFAIK 我知道如何正确执行此操作的唯一方法是调用 .msdn 函数SHGetFolderPath
。
Sample: http://github.com/fab1an/appkit/blob/master/src/main/java/org/appkit/osdependant/OSFolders.java
示例:http: //github.com/fab1an/appkit/blob/master/src/main/java/org/appkit/osdependant/OSFolders.java