import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* Property read tools
*/
public class PropertyReader {
private volatile static PropertyReader instance = null;
private static Properties prop = new Properties();
private PropertyReader() {
}
/**
* sington (double lock check)
*
* @return
*/
public static PropertyReader getInstance() {
if (instance == null) {
synchronized (PropertyReader.class) {
if (instance == null) {
instance = new PropertyReader();
}
}
}
return instance;
}
/**
* Read property from memory first ,if not exits in memory then read file.
* if the key not exit in file ,return "".
*
* @param key
* @return
*/
public String readProperty(String key) {
if (!prop.contains(key)) {
readPropertyFromFile();
}
return prop.getProperty(key) == null ? "" : prop.getProperty(key);
}
/**
* read property from file
*/
private void readPropertyFromFile() {
prop = new Properties();
try {
prop.load(new InputStreamReader(PropertyReader.class
.getClassLoader().getResourceAsStream("application.properties")));
} catch (IOException e) {
e.printStackTrace();
}
}
}