Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

reading from a properties file

 
Greenhorn
Posts: 3
  • 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.
 
Ranch Hand
Posts: 385
  • 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
  • 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.
 
Rancher
Posts: 43016
76
  • 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.
 
Marshal
Posts: 70205
280
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies everyone.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code may help you




Good luck
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • 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
  • 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
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my first post in this topic.
 
    Bookmark Topic Watch Topic
  • New Topic