基本上,我必须通过 Java 应用程序覆盖 .properties 文件中的某个属性,但是当我使用 Properties.setProperty() 和 Properties.Store() 时,它会覆盖整个文件,而不仅仅是那个属性。
我尝试使用 append = true 构建 FileOutputStream,但它添加了另一个属性并且不会删除/覆盖现有属性。
如何对其进行编码,以便设置一个属性覆盖该特定属性,而不覆盖整个文件?
编辑:我尝试读取文件并添加到它。这是我更新的代码:
FileOutputStream out = new FileOutputStream("file.properties");
FileInputStream in = new FileInputStream("file.properties");
Properties props = new Properties();
props.load(in);
in.close();
props.setProperty("somekey", "somevalue");
props.store(out, null);
out.close();