Home > Java > Reading property file in java

Reading property file in java

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DBProperties {

private static final Log log = LogFactory.getLog(DBProperties.class);
private static final String fileName = “database.properties”;
private static Properties properties;
private static DBProperties instance = null;

private DBProperties() {
properties = new Properties();
try {
InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
properties.load(is);
} catch (IOException ioe) {
log.error(“Exception occurred while reading the ” + fileName + “file. ” + ioe.getMessage());
}
}

public static DBProperties getInstance() {
log.info(“GetInstance method of DBProperties class”);
if (null == instance) {
instance = new DBProperties();
}
return instance;
}

public String getProperty(String propertyName) {
return properties.getProperty(propertyName);
}
}

Advertisements
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: