package com.hspedu.io_; import org.junit.Test; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class UpdatePropertiesFile { @Test public void updatePropertiesFile() throws IOException { String filePath = "src\\test.properties"; Properties properties = new Properties(); properties.setProperty("user", "root123"); properties.setProperty("password", "000000"); properties.store(new FileOutputStream(filePath), ""); } }
1、Properties对象的setProperty("key", "value");修改配置文件,如果没有key则新增,如果已经存在key则修改对应的value;
2、store保存方法,第一个参数传入一个输出流OutputStream,第二个参数comments传入字符串注释。