File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Handle Properties

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Taucher
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be a very simple approach (storing/saving destroys the order of course!):

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks peter.let me take a look on the code.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic