使用Properties的setProperty方法并保存到文件
可以通过调用setProperty(String key, String value)方法将新的键值对添加到Properties对象。要保存修改后的属性,可以使用store(OutputStream out, String comments)方法将其写入文件。例如:
Properties properties = new Properties();
properties.setProperty("newKey", "newValue");
try (FileOutputStream fos = new FileOutputStream("config.properties")) {
properties.store(fos, "Updated properties");
} catch (IOException e) {
e.printStackTrace();
}