This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Merging Property files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Merging Property files" Watch "Merging Property files" New topic

Merging Property files

Prasanna Puttappa
Ranch Hand

Joined: Jul 21, 2003
Posts: 54
I have a requirement to merge two property files with features below
1.Maintain order of attributes i.e., order of keys should not be disturbed after merge
2. Comments should be retained.

is there any standard utility api for the above requirement
any suggestions ?

[ August 08, 2005: Message edited by: Prasanna Kumar BP ]

~Prasanna (SCJP5.0, SCWCD5)
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 39529
Sounds like you could use Readers for both files, reading them line by line, and writing out the result in whichever way you need to. Only thing to watch out for would be continuation lines, i.e. properties that span more than one line. Also, be careful to specify the proper file encoding.

Ping & DNS - updated with new look and Ping home screen widget
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Adding to Ulf's post in a somewhat orthogonal but complementary way: consider using LinkedHashMap rather than Properties. This will allow you to maintain insertion order (of course you will need to decide which o the two properties fields comes first) while maintaining the basic contract of a Map (which Properties extends, and if you're using JDK 5.0 there's little reason to continue using Properties anyway). Ulf's post suggests how to read the file; this post suggests where to put the contents of the file after it's been read. Between the two, you may be able to achieve your goals.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Merging Property files
Similar Threads
xdoclet ant task issue
Loading Property files using Struts
validating file uploads
Moving files from server A to server B using JSch
reading the xml files thru java