My Wiki: How to read a properties file in a web application
2012-05-03 11:55
323 查看
My Wiki: How to read a properties file in a web application
Let's assume that you want to read the property named "abc" present in the properties file:
----------------
myApp.properties:
----------------
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?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:
There will just be one line change in the above code:
view plaincopy to clipboardprint?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.
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?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); } }
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?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);
}
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.
相关文章推荐
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- How to successfully edit readonly file in VS?
- In a Web Application and Mobile (hybrid), how to know which this platform running?
- How to read text file in client side via HTML5
- How to Reference and Use JSTL in your Web Application
- How to Identify MDS user in intranet web application ?
- How to use sharepoint webservices in web application
- How to read Json file or text file in Spark
- How to include *.so file in Android-gradle application?
- Shell: How to read lines in a file.
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- How to read Mac .DMG file in Linux or PC
- How do I lock the orientation to portrait mode in a iPhone Web Application?
- How To Read XML File In Java – (DOM Parser)
- How to use AspnetUpload™ in your web application
- How to read and parse CSV file in Java
- How to read Mac .DMG file in Linux or PC(II)
- How To Read a File From Your Application Bundle
- How to properly logout of a Java EE 6 Web Application after logging in
- How to read data from a file in reverse order?