Tuesday, June 10, 2008

Loading a properties file

stolen from http://www.spychalski.de/recipe-6.html


A property file called test.properties in the classpath.

URL url =  ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

System.out.println(p.getProperty("my.property"));


Other method using classpath


It is best to put the properties file in the server's pre-made classpath. You will forget about changing the classpath later on and when you upgrade/change you will get frustrated :)

Anyway, the application's classpath starts in the WEB-INF/classes directory. If you put your propertied in there, the you could use
getClass().getClassLoader().getResourceAsStream(propertyFileName);
to get at it.

If you put it in the same directory as your class (in the class' package) then you could use
getClass().getResourceAsStream(propertyFileName);



No comments: