发布时间:2022-08-09 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印

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:

view plaincopy to clipboardprint?
  1. packageorg.myapp;
  2. importjava.io.IOException;
  3. importjava.io.InputStream;
  4. importjava.util.Properties;
  5. /**
  6. *Simpleclassmeanttoreadapropertiesfile
  7. *
  8. *@authorJaikiranPai
  9. *
  10. */
  11. publicclassMyPropertiesReader{
  12. /**
  13. *DefaultConstructor
  14. *
  15. */
  16. publicMyPropertiesReader(){
  17. }
  18. /**
  19. *SomeMethod
  20. *
  21. *@throwsIOException
  22. *
  23. */
  24. publicvoiddoSomeOperation()throwsIOException{
  25. //GettheinputStream
  26. InputStreaminputStream=this.getClass().getClassLoader()
  27. .getResourceAsStream("myApp.properties");
  28. Propertiesproperties=newProperties();
  29. System.out.println("InputStreamis:"+inputStream);
  30. //loadtheinputStreamusingtheProperties
  31. properties.load(inputStream);
  32. //getthevalueoftheproperty
  33. StringpropValue=properties.getProperty("abc");
  34. System.out.println("Propertyvalueis:"+propValue);
  35. }
  36. }
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:

view plaincopy to clipboardprint?
  1. publicvoiddoSomeOperation()throwsIOException{
  2. //GettheinputStream-->Thistimewehavespecifiedthefoldernametoo.
  3. InputStreaminputStream=this.getClass().getClassLoader()
  4. .getResourceAsStream("config/myApp.properties");
  5. Propertiesproperties=newProperties();
  6. System.out.println("InputStreamis:"+inputStream);
  7. //loadtheinputStreamusingtheProperties
  8. properties.load(inputStream);
  9. //getthevalueoftheproperty
  10. StringpropValue=properties.getProperty("abc");
  11. System.out.println("Propertyvalueis:"+propValue);
  12. }
  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.