GeeCON Prague 2014*
The moose likes Java in General and the fly likes save properties file without affecting existing comments. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "save properties file without affecting existing comments." Watch "save properties file without affecting existing comments." New topic
Author

save properties file without affecting existing comments.

Krishnaa Kumar
Greenhorn

Joined: May 05, 2011
Posts: 25
Hi,

I have a properties file filled up with comments about the key value pair, followed by key value pair.
If, i edit the some key values and save the file, all my comments are lost.
Is there any way to save the new key values with original comments.

-Krishna
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

what do you mean by lost? Are you using any editor to edit the key value pairs?
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

There are no methods on java.util.Properties to handle comments.
They are simply ignored when reading the file.

A comment (one per file) can only be added when using store(...) or storeToXML(...).

You would need to implement a class that reads the file line by line and stores these
lines in the correct order.
The NvPs the need to go into some HashTable...

I think you get the picture.

HTH
Matt

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

I think that the OP is referring to the store() method which writes a file in (probably) hash order and drops any of the original comments. I ended up writing my own Properties work-alike class using a LinkedHashMap to get around this issue.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

My code...





 
GeeCON Prague 2014
 
subject: save properties file without affecting existing comments.