jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes JDK's Bug?! java.util.Properties don't save comments!    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JDK Watch "JDK New topic
Author

JDK's Bug?! java.util.Properties don't save comments!

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi friends:
I want to edit a test.properties file using class java.util.Properties. My file is:



After I run my application,all the comment is lost!
My code is:




After I run my code,the test.properties change to this:


Where is my comments!? They are deleted by java.util.Properties!
All my comments information lost! why Properties class don't save comment? ;-(


When I face C# and Java I choose Java.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I agree with Jesper.


[My Blog]
All roads lead to JavaRanch
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
If you absolutely need to preserve comments when programmatically modifying properties files, have a look at the UberProperties class.


Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JDK's Bug?! java.util.Properties don't save comments!
 
Similar Threads
BufferedReader.readLine() skips lines
Constructor
NoClassDefFoundError
protected field
Reference Variable Casting