Saturday, October 1, 2011

Reading properties file in java

Reading properties file is straightforward using ResourceBundle java util. Below are the codes. ResourceBundle will get the information from the setting-config.properties file. Make sure the properties file is  in the same path as the java class. The data obtained is passed into a map for easy retrieval using key value later.

private HashMap<String, String> propertiesMap = new HashMap<String, String>();

private ResourceBundle resourceBundle = null;


protected void readProperties() {
List<String> propList = new ArrayList<String>();
try {
resourceBundle = ResourceBundle.getBundle("setting-config");
for (Iterator<String> iterator = resourceBundle.keySet().iterator(); iterator.hasNext();) {
String keyString = iterator.next();
propertiesMap.put(keyString, resourceBundle.getString(keyString));
}
}catch (Exception exception) {
System.out.print(exception.getLocalizedMessage());
}
}

No comments:

Post a Comment