Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annoying properties file

 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone got an example of code which extracts a string from a properties file (i.e. a terxt file which contains pairs of values) and prints it out?
I'm having trouble getting such a servlet working.
thanks
James
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai James Hewitt,
I hope you are using java.util.Properties.I think you have problems with the path in which your Property file is situated.Try it by giving the complete path of the file when you are loading the property file
 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, that doesn't seem to fix it. I reckon I'm making a really simple mistake, it's just not obvious to me. Just need to see a simple implementation in a piece of sample code, and I'm sure it'll all be clear.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai James,
See this code and try this
import java.util.Properties;
import java.io.FileInputStream;
public class PropertyExample
{
Properties simpleProperties;
public PropertyExample() throws Exception
{
FileInputStream myFile=new FileInputStream("/home/james/project/files/Info.properties");
simpleProperties=new Properties();
simpleProperties.load(myFile);
}
public String getProperty(String key)
{
return simpleProperties.getProperty(key);
}
}
 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better.
Thanks,
James
 
rajesh katti
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi James,
Just try this....I had probs with properties file..so I tried this with "ResourceBundle" object from util. It is similar to the properties file..Just 1 line of code..
A) Servlet
import java.util.*;
ResourceBundle messages =
ResourceBundle.getBundle("Global_file");
fname = messages.getString("fname");
B)Global_file.properties file
fname = Rajesh
lname = Katti
If u face any probs..do let me know.

Rajesh

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic