This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am starting to play with XML files to store my data and i was wondering if someone could explain to me the logic behind the way it adds information to the file.e.g. i have this simple little code
All works fine apart from the order it stores the information in the file.i.e. the XML file looks like this
Why does it store the data in that order and not a, b, c?
I would prefer it did a ,b ,c rather than b, a, c, mainly for clarity reasons when i am checking it has stored the data properly and the first step towards that goal is understanding why it prints in the order it does.
I guess my obvious next question is tips for ordering it or where to find information on how to order it?
Because Properties extends Hashmap, which is an unordered map.
And so when you extract data from a Properties object and write it to XML, the order of that data is undefined.
Tips for ordering the data: the easiest way to deal with it is to stop wanting it. However if you absolutely must have your data in a specific order in the properties file, then don't use a Properties object.
(And I'm going to move this post out of the Swing forum because it isn't related to Swing at all.)
When you have a properties file, you have two choices. One choice is to maintain it with a text editor, in which case you can put the entries in any way you like. When you read them into a Properties object, they become unordered, but you don't care because the ordering means nothing to the application which uses the properties.
Or you can maintain them with some application, which works with the Properties file and knows what order to put the entries in when it displays them to you. When the Properties object writes them out to the file, they become unordered, but you don't care because you're maintaining them with something which knows what it's doing.
It's when you start using both ideas at the same time that you run into trouble with "unordered" properties. So just pick one of the two approaches and you won't care.
Joined: May 17, 2012
Thanks for the reply Paul. It would seem for future reference that using Properties is not the ideal way so i will look at other ways for transferring data in future, for now having it ordered would have just been nice rather than any form of necessity.
Thanks for moving the post also, i thought i put it in a different section than i did.