Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to retieve the keys and values from a propertyfile in an order  RSS feed

 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following is a part of my code..



n this is my properties file..



bt when i execute this...my output is



how do i get the properties in the same order as in the file...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try Properties#propertyNames()?
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to be frank...
i am totally new to java...n i am trying to learn...
so...could you plese tell me what you meant by Properties#propertyNames()?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used this method to get the property keys

Instead of that, try using

This will return you an Enumeration object (conceptually similar to the Iterator you get in p.iterator();
Check out the API for how to use the Enumeration to get it's elements
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u...
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i gave this :



it still gives the output as

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. My bad. I checked the source code and figured out the property names uses a hashtable underneath.

To get the keys in a sequential order, you will need to a SortedSet. (SortedSet is an interface. So use a TreeSet)
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you thank you thank you... it works now..
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:To get the keys in a sequential order, you will need to a SortedSet.

I don't think so. The problem isn't to get the keys sorted, it's to get them in the same order as they were found in the file. In order of occurrence, not based on any properties of the names themselves. To do this, I think the easiest way is: Swati - forget Properties, and forget TreeSet. Both are useless here. Instead take a look at LinkedHashSet. This gives much of what you need - but what's missing is, we still need a way to load the "properties" from the file.

OK, that's a bit more challenging, but far from impossible. As a first pass, use a BufferedReader or Scanner to read each line of the file. For each line, look for the first occurrence of '='. Everything to the left of that is the key name, and everything to the right is the value for that key. Well, mostly. You might need to trim some whitespace from the beginning and end of each string. And for the values, you might need to consider some special characters and escape characters. Take a look at the JavaDoc for Properties to get an idea what might be found in a standard properties file. However, there's a good chance you don't need to handle all those potential complications in order to get something that's usable for your purposes here.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swati basant wrote:thank you thank you thank you... it works now..

Are you sure? Were the properties really in the same order as they were in the file, or were they in alphabetical order? Or was the file organized in alphabetical order already, in which case the two orders are the same?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
I don't think so. The problem isn't to get the keys sorted, it's to get them in the same order as they were found in the file.


Ahh!!
I created a simple properties file with a=1,b=2,c=3 to test the code. That put me in a blind. Two bads in an hour. Today doesn't seem my day
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ...
it actually jus gives the output in an alphabetical order...

my actual thing is this..
i have a pdf which contains a form...

i have to read the data from the textbox in pdf..(this using the field_name(given as a value) in the property files.)
to retrieve this..i need to get the key from the prop file..

once m done retrieving...i have to populate a java obj...n then...
serialize the object


i have no clue as to how m going to do the whole thing..
i have manged to read from the pdf(for a single field) ..bt then now i have to read for all the fields...n these fields inculde check boxes...radio buttons too..
and using a prop file is mandatory...thats wat my requirement says...



 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'm not sure I understand all of the problem. But consider this: do you actually need to be able to see (or iterate through) all the properties, in the order they were found in the original file, in order to solve your problem? Or do you just need to be able to look up particular properties, one at a time? Is it possible that you were just printing out the properties in order to verify your intermediate results? If so, that's a good thing - but, is it possible that you don't really need the properties in any particular order, as long as you can still look them up?

If so, consider just using Properties as you did originally. And don't worry about the fact that when you print them out, they display out of order. Because there's a good chance that printing them out in order isn't actually one of your requirements.

Of course, if it really is one of your requirements, then feel free to disregard this post.
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea..that would be needed because...

while populating my java object using the setter methods... i would place the setter methods in accordance to the order in the prop file..
for eg..

suppose prop file has
Firstname swati
Lastname basant

and in code...after retrieval i store it in an array ARR say..
then i cud givesomething like...

p1.setFirstname(ARR[0])
p1.setLastname(ARR[1])

is there a way to do this in a simple way... m all confused
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swati basant wrote:yea..that would be needed because...

while populating my java object using the setter methods... i would place the setter methods in accordance to the order in the prop file..

When you say you "would" place the setter methods in that order... is that a requirement, or just something you want to do because it seems right? Because if you are looking for simple solutions, ignoring order and using Properties is pretty simple. Maintaining order using LinkedHashMap is possible, but not as simple. I have already described how you could use LinkedHashMap if you really want to do that. Doing that may not be as simple as you want, but I can't make it simpler. Using Properties and ignoring order is simple, but we can't make the properties appear in order. You can't have both - you can't have "simple" along with "properties in order". You need to pick one.
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm....
placing the setter methods in the order is just for my use...nt a compulsion...

could you tell me..how i cud do it with a linkedhashmap
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!