0

Is there in specific method in java that gets a 'app data' directory where a program in all OS's can store its resources

And if not, is there an easy way to get it? I had noticed a method that let's us check what the name of the OS is, but i am not sure how to imlement that into getting the 'app data'

Thanks in advance!

Socratic Phoenix
  • 556
  • 1
  • 7
  • 19
  • If you're asking where it might be possible to store program configuraiton, espcially writable configuration, then take a look at [this discussion](http://stackoverflow.com/questions/27974857/where-should-i-place-my-files-in-order-to-be-able-to-access-them-when-i-run-the/27974989#27974989). Only windows has "Program Files", Mac uses, at least two, Application directories, so it's not as simple as it sounds. – MadProgrammer Jan 22 '15 at 23:33
  • You should avoid storing writable content in these locations anyway, as under Windows, you might not have writable access to the "Program Files" and under MacOS, the working directory may not be your applications directory. If you want to "bundle" read-only resources with your application, then you can embed them within the application Jar or classpath and use `Class#getResource` or `Class#getResourceAsStream` to read them. How this is achieved will depend on how you are building you application – MadProgrammer Jan 22 '15 at 23:34
  • This [answer](http://stackoverflow.com/a/4851436/1180115) should help for Windows environment :) – Oueslati Bechir Jan 22 '15 at 23:36
  • Sorry for not understanding program files... I am looking for a place to store writable data, ill edit the post. – Socratic Phoenix Jan 22 '15 at 23:47

1 Answers1

3

As of yet there is no single method call. You could use JNA, as seen in this post, but that requires a bit of extra coding. I would recommend just hard-coding it as follows (see here):

private String workingDirectory, OS = (System.getProperty("os.name")).toUpperCase();
if (OS.contains("WIN")) {
    workingDirectory = System.getenv("AppData");
}
else { //Linux or Mac
    workingDirectory = System.getProperty("user.home");
    workingDirectory += "/Library/Application Support";
}

If you just need to store a small to medium amount of data, use java.util.prefs.Preferences.

Community
  • 1
  • 1
jfdoming
  • 975
  • 10
  • 25
  • What i intend to use this for is store a multitude of text files that load text into my program, the number can be almost infinite, but only as the user saves more data. Would preferences then be a viable option? – Socratic Phoenix Jan 22 '15 at 23:52
  • Preferences is meant to be used, as the name implies, as a cross-platform way to save user preferences. While there is no obligation to use it just for that, I would think that, given the potential for a very large amount of data, that you would be better off handling it yourself. – jfdoming Jan 22 '15 at 23:55
  • Storing more data as the user does more work sounds more like a set of documents than a set of preferences. – Jherico Jan 23 '15 at 00:17