查找“应用程序数据”目录在linux和macintosh上 [英] Find directory for "application data" on linux and macintosh

查看:167
本文介绍了查找“应用程序数据”目录在linux和macintosh上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于LWJGL的游戏引擎,要运行它,我需要将所需的本机库放在用户的计算机上。在Windows上,我通过以下方式找到应用程序数据目录:

  System.getenv(APPDATA); 

,一切都很容易,很好。我创建一个File对象,如果需要,调用mkDir,如果文件尚未在机器上,请写入文件。



(注意:创建的目录不应该是临时的文件,因为我想保存提取的文件以供将来运行。此外,创建此目录为保存的游戏和其他此类数据提供了一个简单易用的文件夹。)



但是,如果电脑是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);
}

那么这是正确的方法吗?我正在尝试在Mac上访问应用程序支持(我已经知道这相当于Windows上的AppData文件夹),而且我试图在Linux上使用类似的文件夹,但是我不确定是否用户。家庭找到正确的一个。

解决方案

这应该工作。只需一件事:在linux上,最好将设置保存在用户目录中的隐藏文件夹中。所以对于linux来说,把你的文件夹放在$ HOME / .config下,或者使用来启动它。使它隐藏起来。


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:

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.

(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:

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.

解决方案

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和macintosh上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆