Writing to property file

V

vincente13

Hi there,

I notice that when we try to write a value back to the property file,
the original comment in the property file will be removed.
Any idea how to keep the comment in the property file and yet updating
the key with the new value?

String filename = "property.properties";
Properties p = new Properties();
p.setProperty("CacSrv", "SomeValue");
p.store(new FileOutputStream(filename), null);
 
R

Robert Klemme

Hi there,

I notice that when we try to write a value back to the property file,
the original comment in the property file will be removed.
Any idea how to keep the comment in the property file and yet updating
the key with the new value?

I believe you will have to parse the file yourself to extract the
comment. Alternatively you could use an unused key for the comment.
String filename = "property.properties";
Properties p = new Properties();
p.setProperty("CacSrv", "SomeValue");
p.store(new FileOutputStream(filename), null);

You do not close the FileOutputStream properly. This costs resources
and also might cause inconsistent file content.

robert
 
F

Flo 'Irian' Schaetz

And thus, (e-mail address removed) spoke...
p.store(new FileOutputStream(filename), null);

You are setting your header to null. So the properties don't have a
header... If you want text there, put it there instead of null.

Flo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top