Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

How to Handle Properties

 
Ranch Hand
Posts: 50
  • 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
 
Ranch Hand
Posts: 73
  • 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
 
Ranch Hand
Posts: 174
  • 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
  • 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.

 
Ranch Hand
Posts: 122
  • 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
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks peter.let me take a look on the code.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • 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.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!