Property file are having extension '.property' and keep value in key-value pair. A line in property file can be commented using '#'.
blow is an example of property file,
**********************************************************
# Author: Mohit Singh
# Created On: 16 Apr 2010
# Project: My Test Project
#DB Properties
url=jdbc:odbc:oracle:hostname:1521
dbschema=demodb
username=scott
password=tiger
**********************************************************
Everything that is likely to be changed must be kept in property file. So we will proceed with how to load property files and what is the best way to handle them.
Location of Property File:
Property file are usually kept in src (source) folder but you can create a folder outside to src folder and put your property file there. it's up to your choice but first option is better.
How to load:
Property file can load using class loader. Property file are load in java.util.Property class. Suppose your property file is in src/conf folder. Method blow read property file from given location and returns an object representing property file.
*************************************Approach 1*********************
public Property readPropertyFile()
{
Property appProperty = null;
try {
InputStream is = getClass().getResourceAsStream("/conf/dbproperty.properties");
appProperty = new Property();
appProperty.load(is);
} catch (Exception e) {
e.printStackTrace();
}
return appProperty;
}
*********************************Approach 2*********************
public Property readPropertyFile()
{
Property appProperty = null;
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("conf/dbproperty.properties");
appProperty = new Property();
appProperty.load(is);
} catch (Exception e) {
e.printStackTrace();
}
return appProperty;
}
**********************************************************
Observer carefully both approach, they are far similar, only difference is the way they load and leading slash in approach1. Whatever the way you load the property file, its usage remains same.
How to Read:
Property file are used to get a value against a key. 99% time, you will only be using get() method of Property class which take key string as input and return its value. Let say, we need to test connection to DB,
**********************************************************
public void testDBConnection()
{
Property appProperty = readPropertyFile();
String dburl = appProperty.get("url");
String dburl = appProperty.get("url");
String username = appProperty.get("username");
String password = appProperty.get("password");
String dbschema = appProperty.get("dbschema");
// use these value to make connection to db
String dbschema = appProperty.get("dbschema");
// use these value to make connection to db
**********************************************************
Well, thats All about property file. This article gives you very basic idea of property file. I will provide complete class source code for dealing with property file. Just wait till Monday...
Property files are also used in Internationalization (i18n) of you application. This topic is more relevant to Struts so will cover this aspect in struts tutorial.
For any query, feel free to mail me at mohit.amour@gmail.com
Thanks,
Mohit Singh
No comments:
Post a Comment