替换属性文件的属性

时间:2015-09-07 15:50:46

标签: java

我有一个内容为cluster.properties的文件:

master=1.2
first.node=
second.node=10.20.30.30

我想用一些不同的值替换这些属性。我怎么能这样做?

我的方法看起来像replaceProp(String filePath, String prop, String newValue)

1 个答案:

答案 0 :(得分:0)

这样做的一种方法可能是:

阅读属性:

Properties properties = new Properties();
FileInputStream fis=new FileInputStream("cluster.properties");
properties.load(fis);
fis.close();

替换:

properties.set("master", "1.2.3")
...

保存回来:

FileOutputStream fos=new FileOutputStream("cluster.properties");
properties.store(fos,"Some meaningfule comments");
fos.close();
相关问题