Reputation: 71
In my swing application, I dynamically create components and clicking the save button stores their values to a properties file.
On the value change event of each component, I get the value and put it into a Map. When the save button is clicked, I iterate and store the values to a properties file. Next time I open the window, if the properties file exists, I read the properties from the file and set the values to each component when rendering.
After updating some values again, clicking the save button deletes previous values and the file only contains the changed values.
How can I update the properties file without losing previous values?
Here is my code for writing the properties:
Properties properties = new Properties();
fileOutputStream = new FileOutputStream(propertiesFile);
Iterator itr = attributeMap2.entrySet().iterator();
while (itr.hasNext()) {
Entry<CategoryAttributeObj, String> entry = (Entry<CategoryAttributeObj, String>)itr.next();
properties.setProperty(entry.getKey(), entry.getValue());
}
properties.store(fileOutputStream, "storing index values to properties file");
fileOutputStream.close();
Here is my code for reading the properties:
Properties properties = new Properties();
try {
fileInputStream = new FileInputStream(propertiesFile);
properties.load(fileInputStream);
fileInputStream.close();
}
Upvotes: 0
Views: 3856
Reputation: 1381
You are doing
Properties properties = new Properties();
and then
properties.setProperty(ntry.getKey(),entry.getValue());
I'm going out on my limb and assuming the 'attributeMap2' contains only the updated properties. So essentially you would end up saving only those that are in the attributeMap2 map.
If you want capture only changed properties and still want to update the file, read the existing property files first (before you do the setProperty call) and then set those values that are changed and the store the properties.
Upvotes: 2