欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

My Wiki: How to read a properties file in a web application

程序员文章站 2022-04-19 17:15:06
...

My Wiki: How to read a properties file in a web application

How to read a properties file in a web application

The code to do this is pretty simple. But going by the number of people who keep asking this question, i thought i would post the code over here. Let's consider that you have a war file named SampleApp.war which has a properties file named myApp.properties at it's root :

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:


  1. package org.myapp;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. /** 
  8.  * Simple class meant to read a properties file 
  9.  *  
  10.  * @author Jaikiran Pai 
  11.  *  
  12.  */  
  13. public class MyPropertiesReader {  
  14.   
  15.     /** 
  16.      * Default Constructor 
  17.      *  
  18.      */  
  19.     public MyPropertiesReader() {  
  20.   
  21.     }  
  22.   
  23.     /** 
  24.      * Some Method 
  25.      *  
  26.      * @throws IOException 
  27.      *  
  28.      */  
  29.     public void doSomeOperation() throws IOException {  
  30.         // Get the inputStream  
  31.         InputStream inputStream = this.getClass().getClassLoader()  
  32.                 .getResourceAsStream("myApp.properties");  
  33.   
  34.         Properties properties = new Properties();  
  35.   
  36.         System.out.println("InputStream is: " + inputStream);  
  37.   
  38.         // load the inputStream using the Properties  
  39.         properties.load(inputStream);  
  40.         // get the value of the property  
  41.         String propValue = properties.getProperty("abc");  
  42.   
  43.         System.out.println("Property value is: " + propValue);  
  44.     }  
  45.   
  46. }  
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:



  1. public void doSomeOperation() throws IOException {  
  2.       //Get the inputStream-->This time we have specified the folder name too.  
  3.       InputStream inputStream = this.getClass().getClassLoader()  
  4.               .getResourceAsStream("config/myApp.properties");  
  5.         
  6.       Properties properties = new Properties();  
  7.          
  8.       System.out.println("InputStream is: " + inputStream);  
  9.         
  10.       //load the inputStream using the Properties  
  11.       properties.load(inputStream);  
  12.       //get the value of the property  
  13.       String propValue = properties.getProperty("abc");  
  14.         
  15.       System.out.println("Property value is: " + propValue);  
  16.   }  
  17.     
  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);
    }



That's all that is required to get it working.

posted on 2012-05-03 11:55 lexus 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/05/03/2480385.html