This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help reading a properties file

 
Moieen Khatri
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved this issue.
Thanks to all of you for being so helpful as always!



 
Maurice Le Chat
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • 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;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic