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傳入字符串注釋。