• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

need help reading a properties file

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I might be posting this message in a wrong forum.I have a situation where there is a global variable in the java class files of my application.This global variable stores the path of a folder which is created on the file system:




I need to remove the above hard coded folder path from the java class file and need to use a properties configuration file which will have this path.

Can someone please help me as to what classes/api can I use to implement this?

If someone can post some sample code,I would really be greatful!

Thanks in advance

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moieen,

Follow three simple steps-

1. Define a global variable in the class to hold this propert.



2. Create a property file myPropert.properties in the class path and define the desired value--



3. Read the property file in thge following way--




Thanks,
Tanzy.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moieen Khatri wrote:Can someone please help me as to what classes/api can I use to implement this?



You will need to use Properties API
Check the tutorial here
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Thanks for the replies!! I have problem reading the properties file using the below code :



However,if I specify the path on the file system below I can read the myProperty.properties file



Am I missing some configuration here?

Please advice.

Thanks for your help!
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should put the properties file at the root of the classpath (It is inside your classes folder in this case as you are using in a web application) for your first option.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:You should put the properties file at the root of the classpath (It is inside your classes folder in this case as you are using in a web application) for your first option.


No. Web apps do not have a concept of a default directory, so relative paths (like "myProperty.properties") don't work. What's more, files in the classpath would need to be read by "getResourceAsStream" or something like that, not by creating a FileInputStream.

The file should be put into the WEB-INF directory, and then something like ServetContext.getRealPath("/WEB-INF/myProperty.properties") will give you the correct filename to use.

Instead of hardcoding the filename in the code I'd make it a context attribute in the web.xml file, though (or course, you could do that with the storage path attribute itself, and save yourself the need to keep any properties file at all).
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:You should put the properties file at the root of the classpath (It is inside your classes folder in this case as you are using in a web application) for your first option.



No FileInputStream does not look in Classpath.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please post the code where I can read the .properties file as a resource using the classpath principle.

Thanks in advance
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf has already explained it.

Simple put your property file in classes directory and use getResourceAsStream() .

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I resolved this issue.
Thanks to all of you for being so helpful as always!



 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this kind of purpose JDK includes the ResourceBundle!
look at the following code snippet, which reads a <className>.properties file within the relative dir called resources benath the package dir:
// just be clean on any OS
NL = System.getProperty("file.separator");
ClassLoader cl = Config.class.getClassLoader();
File baseDir = new File(cl.getResource(".").getFile());

// now we now the dir the class Object is actually running in

RUN_DIR = baseDir.getAbsolutePath() + NL;

// could work with Bundles too, but lets use properties

props = new Properties();
ResourceBundle rb = getResourceBundle(Config.class, Locale.getDefault());

// allow comments on the same line within the xx.properties and strip them here

for (String key : rb.keySet()) {
String s = rb.getString(key);
if (s.contains("#")) {
s = s.substring(0, s.indexOf('#')).trim();
}
fill the Properties
props.put(key, s);
}

-----------

ResourceBundle getResourceBundle(Class<?> cls, Locale locale) {
String baseName = classBundleBaseName(cls);
ClassLoader cl = cls.getClassLoader();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, cl);

// could also be simply ResourceBundle.getBundle(baseName);

return bundle;
}
reply
    Bookmark Topic Watch Topic
  • New Topic