aspose file tools*
The moose likes Beginning Java and the fly likes Help with accessing values from a property file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with accessing values from a property file" Watch "Help with accessing values from a property file" New topic
Author

Help with accessing values from a property file

Liana Daughtry
Greenhorn

Joined: Mar 27, 2010
Posts: 13
I am trying to determine the best way to retrieve a value from a property file if my key is stored in a String. The first part of the String will vary depending on input from a user (selected from a dropdown box). For example:

Property file (simple .txt file)
Book1.Title=Some Title
Book2.Title=Some Other Title

User Chooses Book1

String title = (Book1 + ".Title")

Since properties.getProperty(title) will return a null, what are my options?

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what the problem is--if you try to get a property named "Book1.Title" why would it return null?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Something tells me you haven't overridden toString for Book. Check out what System.out.println(Book1); prints.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Liana Daughtry
Greenhorn

Joined: Mar 27, 2010
Posts: 13
Maybe I am missing something or my example wasn't that clear. Let me try again:

If I use the literal value of the String title, "Book1.Title", to access the property file it works (Some Title is returned as expected).

Book1 is actually contained in a form field, let's call it selection. I created a new String by coding String title = (selection + ".Title");

If I code System.out.println(title), "Book1.Title" is returned

What should I code to get the value of Book1.Title from the property file....properties.getProperty(???)

I hope my question is a little more clear. My apologies if it's not. I am pretty new to JAVA (only a couple months in). I have been a COBOL developer for 16+ years and I am finding this transition to be a little difficult. The above seems like it should be so easy, but I can't get it to work.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

??? = title.

If the String value "Book1.Title" is contained in the variable title that's the exact same thing as calling getProperty("Book1.Title").
Liana Daughtry
Greenhorn

Joined: Mar 27, 2010
Posts: 13
Okay, nevermind. I went back to my original code and modified the keys in my property file (had a combinaTION of lower case & caps). I guess that's what happens when you look at something for too long. Should have stepped away. Now I can rest easy for the remainder of the weekend. Thanks for taking time to respond.


properties.getProperty(selection + ".Title");
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No problem, and welcome to JavaRanch :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with accessing values from a property file