File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes reading from a properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "reading from a properties file" Watch "reading from a properties file" New topic
Author

reading from a properties file

sk devi
Greenhorn

Joined: Mar 26, 2010
Posts: 3
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

Joined: Sep 19, 2008
Posts: 385

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

You may try using Reg-exp with IO package....


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
sk devi
Greenhorn

Joined: Mar 26, 2010
Posts: 3
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
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
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.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Surely the key isn't "id" but "1" or "2" or "3"?

And welcome to the Ranch
sk devi
Greenhorn

Joined: Mar 26, 2010
Posts: 3
Thanks for your replies everyone.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
This code may help you




Good luck
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
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

Joined: Sep 19, 2008
Posts: 385
Can you please let me know how would you approach in such a case?

it will be helpful for me in future.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
See my first post in this topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading from a properties file
 
Similar Threads
properties file
Property file is cached
Reading key/value pairs from a .properties file and creating objects
How to read properties file in netbeans?
How do you setup a Publisher Signature