How do I load property settings with the Properties class?

Luigi Viggiano

java.util.Properties objects can load values from a file using the method load(InputStream).

Here is the code you need:

Properties props = new Properties();
props.load(new FileInputStream("propertyfile.properties"));
String value = props.getProperty("propertyname");
//Just a trick: in a web archive (war) you can get the InputStream inside the war archive using 
ClassLoader cl = this.getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("it/package/application.properties");

This is better than using a FileInputStream, because you are loading the file within the archive as it was a resource. You should use this.getClass().getClassLoader() to use the same ClassLoader as the one used the servlet container to load your JSP/Servlet. This code is snipped from a JSP page inside Tomcat.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact