修改.properties配置文件中值

案例: 修改短信系统-短信接口中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为新密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值