Monday, May 28, 2012

Read File From /WEB-INF/ at runtime

Suppose one want to read a  example.properties  file located in /WEB-INF/props/example.properties in run time.

A simple way :

Properties properties = new java.util.Properties();
        InputStream inputStream =   
            getServletContext().getResourceAsStream("/WEBINF/props/example.properties");
properties.load(inputStream);

Now , we are ready to get value from example.properties file .

String value = properties.getProperty("propertyName");

Sample Properties File :

example.properties 

propertyName  = 123
-------------------------------------------------------------------

getResourceAsStream(String str) explained at java doc  :


public InputStream getResourceAsStream(String name)
Returns an input stream for reading the specified resource.The search order is described in the documentation for getResource(String).
Parameters:
name - The resource name
Returns:
An input stream for reading the resource, or null if the resource could not be found
Since:
1.1

No comments:

Post a Comment