[英]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. 由于我认为超出此问题范围的原因,我想移动我编写的用于Java Web Start的程序以停止使用JWS并将其作为普通的Jar文件分发。
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). 这个计划的主要问题是如何确定在Java中以一种不错的方式存储文件的位置,假设我将在不同的平台上运行(虽然我也依赖于JOGL,所以没有很大的风险可靠地运行深奥的平台,至少)。 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. 我目前使用JNLP PersistenceService
来实现这个目的,所以搬出JWS我需要找出一些存储文件的目录。 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. 我认为这在Unix和类Unix平台上运行良好,但至少在Windows等平台上显然很难看 ,我想我应该使用AppData
目录或者再次调用它。
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. 我目前的想法是使用这个datadir
作为默认的回落,除了在已知的平台上,我用更好的东西替换它,比如Windows。 The questions I have about this are these: 我对此的问题是:
os.name
, which I guess should work well enough, but is there a better way? 我现在能os.name
的最好的事情是将一些模式与os.name
相匹配,我认为它应该运行得很好,但是有更好的方法吗? 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. 我本质上不介意用反射进行测试,以查看是否存在一些可能有所帮助的sun.*
包(具有回退到默认值的风险),如果这是好的话。 (Question applies in general to all platforms; are there any somewhat robust idioms for figuring this out in general?) (问题一般适用于所有平台;是否有任何一些有用的习惯用法来解决这个问题?) datadir
should work fine on OSX, right? 我的datadir
应该可以在OSX上正常工作,对吗? 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来实现此目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.