What I need is a platform-independent way of obtaining the path to the local application data directory. System.getenv("LOCALAPPDATA")
seems to work only with Windows. How do I go about this?
You could probably say something like (contradict me if I am wrong, or if this a bad approach)
private String workingDirectory; //here, we assign the name of the OS, according to Java, to a variable... private String OS = (System.getProperty("os.name")).toUpperCase(); //to determine what the workingDirectory is. //if it is some version of Windows if (OS.contains("WIN")) { //it is simply the location of the "AppData" folder workingDirectory = System.getenv("AppData"); } //Otherwise, we assume Linux or Mac else { //in either case, we would start in the user's home directory workingDirectory = System.getProperty("user.home"); //if we are on a Mac, we are not done, we look for "Application Support" workingDirectory += "/Library/Application Support"; } //we are now free to set the workingDirectory to the subdirectory that is our //folder.
Note that, in this code, I am taking full advantage that Java treats '/'
the same as '\\'
when dealing with directories. Windows uses '\\'
as pathSeparator, but it is happy with '/'
, too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA");
and it would have worked just as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With