views:

62

answers:

2

Hi, 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 properties 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

+3  A: 

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.

Jon Skeet
I am keeping the output stream open all the time. Should I be creating a new one for every overwrite?
Aly
@Aly: I would. In general it's a good idea to open/write/close in a small work unit, so that most of the time you don't have an open file handle.
Jon Skeet
A: 

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

Mew 3.2