【问题标题】:replace properties of properties file [duplicate]替换属性文件的属性[重复]
【发布时间】:2015-09-07 15:50:46
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你知道如何实际读写文件吗?如果你这样做应该很容易。如果您不这样做,Google 搜索一下就有无数的教程/SO 问题...
  • 谢谢!我在stackoverflow.com/questions/15337409/…得到了一些答案

标签: java


【解决方案1】:

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

读取属性:

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();

【讨论】:

  • 关闭流应该在 finally 块/try-with-resources 语句中完成。
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2022-01-24
  • 2018-05-06
相关资源
最近更新 更多