File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes need help reading a properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help reading a properties file" Watch "need help reading a properties file" New topic
Author

need help reading a properties file

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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

Joined: Jul 19, 2009
Posts: 110
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.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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

Joined: Mar 24, 2008
Posts: 3775

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
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
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).


Ping & DNS - updated with new look and Ping home screen widget
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

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

Joined: Nov 27, 2007
Posts: 144
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

Joined: Jan 01, 2009
Posts: 874


Ulf has already explained it.

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

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
I resolved this issue.
Thanks to all of you for being so helpful as always!



Maurice Le Chat
Greenhorn

Joined: Jan 20, 2009
Posts: 18
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;
}

SCJP 5
 
jQuery in Action, 2nd edition
 
subject: need help reading a properties file
 
Similar Threads
Need Help for implementing save as dialog box for downloading a folder not a file
Newer version of java
Opening a file with relative path
Defining Ant build.xml java properties files for a run targer
Adding class-path to manifest