There are several options to handle the Java properties files:
The properties files can be stored in the classpath. This way they can be put inside jar files and it’s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn’t work:
Properties properties = new Properties() ;
URL url = ClassLoader.getSystemResource("test.properties");
properties.load(new FileInputStream(new File(url.getFile())));
The following example works to load the properties files in a web application. This snippet was tested on Tomcat 5.5. The ‘/’ represents the root of the class path. Otherwise the properties file location is considered relatively to “this” class (or to MyClass for the second example):
Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("/seoimproved.properties"));
Similar example to use in a static context:
Properties properties = new Properties() ;
properties.load(MyClass.class.getResourceAsStream("/seoimproved.properties"));
The properties files can be loaded from any location.
Properties properties = new Properties() ;
properties.load(new FileInputStream("C:\\tmp\\test.properties"));
9 Responses
Franklin
24|Oct|2008 1What do you mean by “the root of the class path”? Do you mean the default package location? WEB-INF/src (or WEB-INF/classes)?
admin
25|Oct|2008 2yes. I don’t have the src folder in WEB-INF. When the aplication is deployed the java files are compiled in WEB-INF/classes and the resources are simply copied there.
So I put the properties files directly in the src folder.
stefano
27|Nov|2008 3and how to write a properties in classpath?
i can read but not wite.. thanks
admin
27|Nov|2008 4Actually is not possible to write properties files in classpath, because the properties are picked up by jvm and they are read as resources. It’s the JVM to control when they are loaded and once loaded they are reloaded. In less words, it’s not possible as I know.
Of course you can modify the files, but not using them as classpath resources, and you can not use the reading methods I described earlier;l sorry for the misleading title.
Initially I intended to put all the read/write methods for property files but I ended putting only those I’m using.
nagarajan
30|Sep|2009 5finding the path of the property file in web application is explained very well.Thanks!
ranganath
09|Dec|2009 6read worth.
ravil
10|Dec|2009 7Thanks a lot for your brief instructions. The are very helpful!!
John, the Fisherman
10|Jan|2010 8Saved my day! I was having problems trying to load the file using a relative path in my web app. Also, I did not remeber the existence of the Properties class.
Sandeep
02|Feb|2010 9Thank you very much,it helped me a lot
Leave a reply
Search
Categories
Blogroll
Recent Posts
Recent Comments
Tags
A design creation of Design Disease
Copyright © 2007 - FactoryPattern.com - is proudly powered by WordPress.
InSense 1.0 Theme by Design Disease