• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annoying properties file

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much better.
Thanks,
James
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic