A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Spring in Action
this week in the
JDK's Bug?! java.util.Properties don't save comments!
Joined: Feb 17, 2003
Jul 19, 2006 00:52:00
I want to edit a test.properties file using class
. My file is:
#my comment here test1=www.li.com #my comment 2 here test2=www.liren.com #my comment 3 here test3=www.lyo.com
After I run my application,all the comment is lost!
My code is:
InputStream in=....... Properties p=new Properties(); p.load(in); p.setProperty("test","hi"); p.store(new FileOutputStream("test.properties"),"changed");
After I run my code,the test.properties change to this:
#changed test1=www.li.com test2=www.liren.com test3=www.lyo.com
Where is my comments!? They are deleted by
All my comments information lost! why Properties class don't save comment? ;-(
When I face C# and Java I choose Java.
Jesper de Jong
Joined: Aug 16, 2005
Jul 19, 2006 01:08:00
The specifications of the Properties class and the load(...) and store(...) methods do not say anything about preserving your comments in the file.
This is not a JDK bug - the Properties class was just not designed to save files with comments etc. in exactly the same format as what you typed in by hand.
Java Beginners FAQ
JavaRanch SCJP FAQ
The Java Tutorial
Java SE 8 API documentation
Joined: Nov 24, 2005
Jul 19, 2006 01:11:00
I agree with Jesper.
All roads lead to JavaRanch
Joined: Mar 22, 2005
Jul 19, 2006 01:52:00
If you absolutely need to preserve comments when programmatically modifying properties files, have a look at the
Ping & DNS
- my free Android networking tools app
I agree. Here's the link:
subject: JDK's Bug?! java.util.Properties don't save comments!
BufferedReader.readLine() skips lines
Reference Variable Casting
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014