廣告贊助

原文:http://jaitechwriteups.blogspot.tw/2007/01/how-to-read-properties-file-in-web.html

SampleApp.war
   |
   |-------- myApp.properties
   |
   |-------- WEB-INF
                |
                |---- classes
                         |
                         |----- org
                                 |------ myApp
                                           |------- MyPropertiesReader.class
                                           

Let's assume that you want to read the property named "abc" present in the properties file:

----------------
myApp.properties:
----------------
abc=some value
xyz=some other value


Let's consider that the class org.myApp.MyPropertiesReader present in your application wants to read the property. Here's the code for the same:
package org.myapp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Simple class meant to read a properties file
 * 
 * @author Jaikiran Pai
 * 
 */
public class MyPropertiesReader {

    /**
     * Default Constructor
     * 
     */
    public MyPropertiesReader() {

    }

    /**
     * Some Method
     * 
     * @throws IOException
     * 
     */
    public void doSomeOperation() throws IOException {
        // Get the inputStream
        InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("myApp.properties");

        Properties properties = new Properties();

        System.out.println("InputStream is: " + inputStream);

        // load the inputStream using the Properties
        properties.load(inputStream);
        // get the value of the property
        String propValue = properties.getProperty("abc");

        System.out.println("Property value is: " + propValue);
    }

}



Pretty straight-forward. Now suppose the properties file is not at the root of the application, but inside a folder (let's name it config) in the web application, something like:

SampleApp.war
   |
   |-------- config
   |           |------- myApp.properties   
   |           
   |
   |-------- WEB-INF
                |
                |---- classes
                         |
                         |----- org
                                 |------ myApp
                                           |------- MyPropertiesReader.class
                                           


There will just be one line change in the above code:


  public void doSomeOperation() throws IOException {
        //Get the inputStream-->This time we have specified the folder name too.
        InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("config/myApp.properties");
        
        Properties properties = new Properties();
         
        System.out.println("InputStream is: " + inputStream);
        
        //load the inputStream using the Properties
        properties.load(inputStream);
        //get the value of the property
        String propValue = properties.getProperty("abc");
        
        System.out.println("Property value is: " + propValue);
    }
創作者介紹
創作者 瑞、瑪姬與小昆妮 的頭像
瑞、瑪姬與小昆妮

瑞瑪姬與小昆妮♫趴趴走黑白吃

瑞、瑪姬與小昆妮 發表在 痞客邦 留言(0) 人氣()