This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading from a properties file

 
sk devi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to read from a properties file:

test.properties:

id=1
name=abc
dept=xyz
id=2
name=efg
dept=asd
id=3
name=gh
dept=yu


In my code, i will be passed one id value, I will have to read the properties file to get the id's corresponding name and value properties. Can any one tell me how i can write the java code to accomplish that?

I wrote something like this, but apparently this doesn't work because it seems like the keys contains only unique values and my property file has more than key.
//inputId is the input id value for which i am trying to match the name and dept
Enumeration<Object> enuKeys = properties.keys();
String key = null;
String value =null;
while (enuKeys.hasMoreElements()){
key = (String) enuKeys.nextElement();
if (key.contains("id")){
value = properties.getProperty(key);
System.out.println("value is "+value);
System.out.println("id is "+id);
if (value.equals(inputId)){
System.out.println("values are equal");
enuKeys.nextElement();
name= properties.getProperty("name");
enuKeys.nextElement();
dept= Integer.parseInt(properties.getProperty("dept"));
break;
}else {
continue;
}
}else{
continue;
}
}

Thanks in advance.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Use code tag when you post code.....

You may try using Reg-exp with IO package....
 
sk devi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Siva,
I am not familiar with REG exp. Do you have an example of how I can do this in code?
Thanks.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use properties, you could do something like this:

Then you can read the "numEntries" property, and subsequently read id, name and dept from 1 to numEntries. If the IDs don't have special meaning you could omit them altogether.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the key isn't "id" but "1" or "2" or "3"?

And welcome to the Ranch
 
sk devi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies everyone.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code may help you




Good luck
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:This code may help you

I wouldn't do that - it's really brittle code. There's no reason not to use properties here, as long as they're meaningfully structured.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please let me know how would you approach in such a case?

it will be helpful for me in future.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my first post in this topic.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic