/**
* 读取属性配置文件
* @param key 键
* @return
*/
public static String getProperties(String key) {
Properties props = new Properties();
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
FileInputStream fis = null;
try {
fis = new FileInputStream("resource/dataSource.properties");
props.load(fis);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null!=fis) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 设置配置文件
* @param parameterName 键
* @param parameterValue 值
*/
public static void setProperties(String parameterName,String parameterValue) {
Properties prop = new Properties();
prop.setProperty(parameterName, parameterValue);
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
FileInputStream fis = null;
OutputStream fos = null;
try {
// fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);
// 从输入流中读取属性列表(键和元素对)
fis = new FileInputStream("resource/dataSource.properties");
prop.load(fis);
fis.close();//一定要在修改值之前关闭fis
fos = new FileOutputStream("resource/dataSource.properties");
prop.store(fos, "");//第二个参数为注释
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null!=fos) {
try {
fos.close();
if(null!=fis) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}