1、在src目录下创建application.properties文件;
2、创建读取资源文件的类
public class PropertiesUtil {
private static PropertiesUtil instance;
/* 默认文件名称 */
private final static String fileName = "application.properties";
private static Properties properties;
/**
* 初始化
*/
public static void init() {
if (null == instance) {
instance = new PropertiesUtil();
}
if (null == properties) {
properties = new Properties();
// 读取资源文件
InputStream inputStream = PropertiesUtil.class.getClassLoader()
.getResourceAsStream(fileName);
try {
properties.load(inputStream);
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
public static PropertiesUtil getInstance() {
init();
return instance;
}
/**
* 读取资源文件
*
* @param key关键字
* @return
* @throws Exception
*/
public String getProperties(String key) {
try {
if (null == properties) {
init();
}
return properties.getProperty(key);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}