开发者

save state by overwriting a properties file

开发者 https://www.devze.com 2022-12-19 05:35 出处:网络
I am using a java properties file to store some config information, on Object creation I am loading this information, then at certain points in the objects lifetime I want to overwrite the values in t

I am using a java properties file to store some config information, on Object creation I am loading this information, then at certain points in the objects lifetime I want to overwrite the values in the propertie开发者_JS百科s file so that next time an object of this type is instantiated it is configured with current information. the setProperty method doesn't seem to be doing this and when i use the store method it just appends a new (updared) copy of the file to the end of the original. Any ideas?

Thanks


setProperty certainly won't do this automatically (unless you use your own type which overrides it).

store should be fine - but my guess is that you're opening an output stream (or writer) for append rather than overwrite. Are you holding the output stream open all the time? Or perhaps you're using new FileOutputStream(path, true) which will append? Without showing us any code, it's hard to know what you're doing wrong.

As WizardOfOdds says, the preferences API may well be a better idea in the first place.


Why don't you try to serialize your objects and save them when you want. Using Serializable objects you can simply save the state of an object any time you want to. and next time when you want to load it you can read it from the file and initialize the object with this value... Check this site: http://java.sun.com/j2se/1.4.2/docs/guide/serialization/index.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号