Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

java - Find directory for "application data" on linux and macintosh

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...