案例: 修改短信系统-短信接口中password。
private static Properties properties;
/**
* 获取路径
* @return path
*/
private static String getpath() {
// path="D:\\Program Files\\webwork\\slyw\\WEB-INF\\classes\\ky_empp.properties";
String path=SortMsgUtil.class.getClassLoader().getResource("").getPath();
try {
path = java.net.URLDecoder.decode(path,"utf-8");
path = path.substring(1, path.length())+"ky_empp.properties";
} catch (Exception e) {
log.info("读取路径出错:\n" + e.getMessage());
}
return path;
}
/**
* 获取 旧密码
* @return password
*/
public static String getPassword()
{
String path=getpath(); //拿到路径
File f=new File(path);
properties = new Properties();
try {
InputStream is=new FileInputStream(f); .//拿配置文件中的值需用InputStream
properties.load(is);
} catch (Exception e) {
log.info("读取empp信息出错:\n" + e.getMessage());
}
return properties.getProperty("empp.password");
}
/**
* 短信帐号配置文件更改密码
*/
public static void password(String pw) {
String key="empp.password";
try {
String path=getpath();
OutputStream os=new FileOutputStream(path); .//赋值给配置文件中的值需用 OutputStream
// 找到key值,修改value !
properties.setProperty(key, pw);
properties.store(os, "Update '" + key + "' value"); // 在配置文件中注释更新那个属性
os.close();
} catch (Exception e) {
log.info("设置empp信息出错:\n" + e.getMessage()); } }
public static void main(String[] args) {
getPassword(); //旧密码
password("123");// 123为新密码
}