I'm trying to use Property files in java, using java.util.Properties class.
I can load them ok from the file I want and work around with them using getProperty, setProperty methods. The problem arises when I'm trying to write them to disk.
This example, my property file:
# Important Comment1
key1=A
#Important Comment2
key2=B
And my code to change the value of key1 into "C":
properties.load(new java.io.FileInputStream(propertyFilePath));
propFile.setProperty("key1","C"
;
properties.store(new FileOutputStream(propertyFilePath), null);
I think this is the way of doing it, and it works, but there are 2 things that happen and I'd like to know if I can avoid them:
1-All the comments on my original properties file are erased
2-The order of the keys in the new property file isn't the same as in the original one (ie for example key2 would appear b4 key1 in the file, in this case maybe not but my original file as more than 10 keys and this happens everytime)
What I'd like to do is to write the new edited property file just like the first one, maintaing their comments and order of keys, just altering the values.
Comments and order are really important here since I can't have sections inside property files like in the .ini files used in VB or C++.
Any help is appreciated
TIA
I can load them ok from the file I want and work around with them using getProperty, setProperty methods. The problem arises when I'm trying to write them to disk.
This example, my property file:
# Important Comment1
key1=A
#Important Comment2
key2=B
And my code to change the value of key1 into "C":
properties.load(new java.io.FileInputStream(propertyFilePath));
propFile.setProperty("key1","C"
properties.store(new FileOutputStream(propertyFilePath), null);
I think this is the way of doing it, and it works, but there are 2 things that happen and I'd like to know if I can avoid them:
1-All the comments on my original properties file are erased
2-The order of the keys in the new property file isn't the same as in the original one (ie for example key2 would appear b4 key1 in the file, in this case maybe not but my original file as more than 10 keys and this happens everytime)
What I'd like to do is to write the new edited property file just like the first one, maintaing their comments and order of keys, just altering the values.
Comments and order are really important here since I can't have sections inside property files like in the .ini files used in VB or C++.
Any help is appreciated
TIA