Reading property file using ResourceBundle
Now it’s very easy to read the .properties file using ResourceBundle utility.
Step 1 : Create a properties file, i.e. key.properties
name=Amit
email=amitpatelinfo@gmail.com
Step 2 : Create a Property reader class. i.e. PropertyReader.java
package com.ap.util;
import java.util.ResourceBundle;
/**
* Read the properties file.
* @author Amit Patel
*
*/public class PropertyReader {
private static final ResourceBundle keyBundle = ResourceBundle.getBundle(“key”);
public static String getValueFromKeyFile(String key){
try{
return keyBundle.getString(key);
}catch(Exception ex){
return null;
}
}
}
Step 3 : Use the PropertyReader to read the property file.
String email= PropertyReader.getValueFromKeyFile(“email”);
That’s it. .. Wow you read the property file using few line of code.. Great..!