3

For reasons that I guess are outside the scope of this question, I want to move a program that I have written for use with Java Web Start to stop using JWS and distribute it as an ordinary Jar file instead.

My main problem with this plan is how to figure out where to store files in a nice manner in Java, assuming I'm going to run on different platforms (though I depend on JOGL as well, so there's no great risk of running on horribly esoteric platforms, at least). My program stores various pieces of local data, mostly for caching or "caching-like" purposes -- it's extremely nice to be able to keep it, but it's not a complete disaster if the data is lost.

I currently use the JNLP PersistenceService for this purpose, so moving out of JWS I'll need to figure out some directory to store files in instead. Consider this:

File datadir = new File(System.getProperty("user.home"), ".myprogram");

I figure this works well on Unix and Unix-like platforms, but it's clearly ugly, at least, on some platforms like Windows where I guess I should use the AppData directory or whatever it's called again.

My current line of thought is to use this datadir as a default fall-back everywhere except on known platforms where I replace it with something better, like Windows. The questions I have about this are these:

  • Is this a reasonable default to begin with? I'm kind-of-fine with this failing on some unknown platforms; I'll just silently disable caching and some extended features if this is the case, but could it fail in some manner that leaves actively bad results?
  • Is there any good way to figure out I'm on Windows? The best thing I can figure out right now is to match some patterns against os.name, which I guess should work well enough, but is there a better way? I don't intrinsically mind testing with reflection to see if some sun.* packages exist that could help out or something (with the risk of falling back to defaults), if that is good. (Question applies in general to all platforms; are there any somewhat robust idioms for figuring this out in general?)
  • Is there a good way to figure out the directory to use on Windows? Using static paths seems ugly since I know they can be overridden with registry settings, or be localized and whatnot.
  • Are there any other platforms I should think of from the outset? My datadir should work fine on OSX, right?
  • Is there any better alternative altogether?
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • But what's wrong with user.home? That makes your application/game multi-user friendly. It works on Windows too, users have a home directory there too where you are guaranteed you can actually write files. Its a strategy I have seen several Java applications use, including IDEs. Of course the most flexible way is to let the user choose the location. – Gimby Aug 13 '14 at 14:40
  • @Gimby: I don't know about Windows, but at least on Unix it is quite frowned upon to just create a random bunch of files directly in a user's home directory. Forcing the user through UI to choose the location is kinda ugly for files used for internal, technical purposes, wouldn't you say? – Dolda2000 Aug 15 '14 at 06:28

2 Answers2

1

If it's just cached data that is replaceable I would just recommend using the temp directory.

See:

new File(File.createTempFile().getParent(), 'my-file-name');

You can set it to be destroyed on your app shut down or just leave it there.

If you want more of a semi-permanent but out of the way storage you may need to care what OS you're on and act accordingly like the answers to this question

Community
  • 1
  • 1
Randyaa
  • 2,935
  • 4
  • 36
  • 49
  • Well, in that case I wouldn't know by what name to open it by the next time I come back, would I? :) – Dolda2000 Aug 13 '14 at 00:51
  • 1
    You can do something like new File(File.createTempFile().getParent(), 'myfile' ) and it'll always be named the same and in the temp directory. – Randyaa Aug 13 '14 at 00:54
  • Either way, I wouldn't consider the data so temporary that I'd feel fine with storing it in `/tmp`. It would be nice to keep across reboots and whatnot. I'll be looking at the answer you linked. – Dolda2000 Aug 13 '14 at 00:55
1

Consider using the java.util.prefs.Preferences API for this purpose.

Steve C
  • 18,876
  • 5
  • 34
  • 37