放在WEB-INF/classes/下的xxx.properties文件中,然后可以通过以下方法读取:
1)xxx.properties的内容:
key=value
2)Configuration.java的内容:
public class Configuration {
private Properties properties;
private final static Configuration cfg = new Configuration();
private Configuration() {
properties = new Properties();
InputStream is = null;
try {
is = getClass().getResourceAsStream("/xxx.properties");
properties.load(is);
} catch (Exception exception) {
System.out.println("Can't read the properties file. ");
} finally {
try {
if (is != null)
is.close();
} catch (IOException exception) {
// ignored
}
}
}
/**
* Use singleton pattern, only return one instance of Configuration.
* @return Configuration
*/
public static Configuration getInstance() {
return cfg;
}
/**
* Retun a value for certain key.
* @param key a certain key define in properties file.
* @return value
*/
public String getValue(String key) {
return properties.getProperty(key);
}
}
3):
Configuration cfg = Configuration .getInstance();
String value = cfg.getValue("key");