简体   繁体   中英

Figuring out where to store local files in Java

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?

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 t his question

考虑使用java.util.prefs.Preferences API来实现此目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM