Java中利用properties类修改替换properties类型文件中的键值对

来源:互联网 发布:淘宝店的前景 编辑:程序博客网 时间:2024/04/30 18:29
--------------------------------------题记
最近公司需要做一个功能:在web项目中,利用网页动态修改jdbc中的连接参数。那么其中除了直接更改spring容器中的bean的值,还有就是修改jdbc.properties中的连接参数键值对。那么如何达到替换某一对键值对中键所对应的值呢?

--------------------------------------代码

String password = request.getParameter("password");String path = request.getSession().getServletContext().getRealPath("/") + "WEB-INF\\classes\\cfg\\properties\\jdbc.properties";InputStream in = null;OutputStream out = null;try {Properties prop = new Properties();in = new FileInputStream(path);prop.load(in);//存入输入流到prop键值对prop.setProperty("jdbc.password",password); //更改或保存键值对out = new FileOutputStream(path); prop.store(out, null); //把prop键值对放入out输出流} catch (Exception e) {e.printStackTrace();} finally {in.close();out.close();}


原创粉丝点击