properties 文件操作
一、路径获取:
(1)绝对路径的获取
File systemPropertiesFile=new File("/Users/liushuaic/git/yikangPortal2/yikangportal/
src/main/webapp/WEB-INF/classes/system.properties");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(systemPropertiesFile);
systemProperties.load(fileInputStream);
systemProperties.getProperty("invitationUrl");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
(2)获取当前类路径
String currentPath=SystemProperties.class.getResource(".").getPath();
输出:/Users/liushuaic/git/yikangPortal2/yikangportal/src/main/webapp/WEB-INF/classes/com/yikang/base/utils/ 当前的类路径
(3)通过相对路径获取
String filePath=SystemProperties.class.getResource("/system.properties").getPath();
输出:/Users/liushuaic/git/yikangPortal2/yikangportal/src/main/webapp/WEB-INF/classes/system.properties 得到了 WEB-INF/classess 下的system.properties文件
二、加载properties 文件
// 获取WEB-INF 下的 system.properties 文件
InputStream inputStream=SystemProperties.class.getResource("/system.properties").openStream();
systemProperties.load(inputStream);
System.out.println(systemProperties.getProperty("invitationUrl"));
/**
* @author liushuaic
* @date 2015/12/10 11:55
* @desc system配置信息
* **/
public class SystemProperties {
private static Properties systemProperties=new Properties();
static {
try {
InputStream inputStream=SystemProperties.class.getResource("/system.properties").openStream();
systemProperties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author liushuaic
* @date 2015/12/01 11:54
* @desc 获取systemProperties 文件中的值
* */
public String getPropertieValue(String key){
return systemProperties.getProperty(key);
}
}