wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to Handle Properties 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 » Java in General
Bookmark "How to Handle Properties" Watch "How to Handle Properties" New topic
Author

How to Handle Properties

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Hi.

I have a Property file.

Need to get All the Keys in that File.

I have took all keys using properties.keySet();

But i need the keys in same order which i have entered.

Key1
Key2
Key3

the Output comes as Key2 Key3 Key1. I know Set is unordered. How to get Ordered.

Thanks in advance


N.Senthil Kumar
Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73
Hi Kumar,
Unfortunately i don't know the answer of your specific question to ordering the set, but i have worked with Properties files/objects and from my experience, you can not even be sure that they appear in the file as same order as you write them!
for instance, i had similar situation in my application:

but as i open the Properties file by using Wndows NotePad, it appears as ordered below:
key1=value1
key2=value2
key4=value4
key3=value3
...
But ordering a set may be possible, eventhough i don't know how!

Best luck

I'm really tired of being engaged with stuff other than Java and programming

Wish to get back soon to my love...
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
I don't think that this can be achieved in an easy way. For this reason (ok, at least it was part of it ; - ) I implemented my own extended Properties class (that remembers the order of keys like they occur in the properties file) sometime ago.

So do you need the specific order from the properties file or could you just sort the keys in some way, to achieve your goal?


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Peter Taucher wrote:I don't think that this can be achieved in an easy way. For this reason (ok, at least it was part of it ; - ) I implemented my own extended Properties class (that remembers the order of keys like they occur in the properties file) sometime ago.

So do you need the specific order from the properties file or could you just sort the keys in some way, to achieve your goal?



Thanks for your reply , friends..




I need to get the Keys in specific order from the Properties file.

Mark Wa
Ranch Hand

Joined: May 29, 2009
Posts: 122
If they are named key1 key2 key3 etc you could just place them in a list and run a sort algorithm.

Or maybe if you know the key names refer to them by name?
Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73
If you just intend to have the pairs as same order as they appears in file (and not as same as you write them by setProperty method, as i mentioned), you may have another option:
read the file by means of a character stream, use BufferedReader.readLine() to catch pairs, and use "=" as delimiter to seperate the key and the vslue !

Best of luck
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
That would be a very simple approach (storing/saving destroys the order of course!):

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
thanks peter.let me take a look on the code.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Make sure when posting code to remove extraneous comments.

I also avoid things like "if (foo == false)" in favor of "if (!foo)", the Java convention. The inline comments really belong in the method's Javadocs. The chopping of key values belongs in a separate utility method, but I'm not convinced it should be there at all, since it's potentially misleading... and the two list methods should be refactored.

And, in general, we try not to post complete solutions, but rather provide hints and guidelines.
 
jQuery in Action, 2nd edition
 
subject: How to Handle Properties
 
Similar Threads
Any replacement for ResourceBundle
How to convert query string into xml Structure
Hashtable question!
midlet.platformRequest(string) problem for multiple request parameters in url
please help with text formating in java