store有两个成员方法
void store(OutputStream out , String comments)
void store(Writer writer,String comments)
参数: OutputStream out :字节流输出,不能写中文 Writer writer : 字符流输出,可以写中文 String
comments:注释,用来解释说明保存的文件是做什么用的,不能使用中文,会产生乱码。默认是unicode编码,一般使用""空字符串
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Properties;
public class PropTest {
public static final String FILE_PATH = "./src/file.ini";
public static void main(String[] args) {
// Properties pps = System.getProperties();
// pps.list(System.out);
Properties pps = new Properties();
try {
// pps.load(new FileInputStream(FILE_PATH));
pps.load(new FileReader(FILE_PATH));
Enumeration<?> enumeration = pps.propertyNames();
while (enumeration.hasMoreElements()) {//读取
String strKey = (String) enumeration.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + " = " + strValue);
}
// 写入
pps.setProperty("枫雅", "大王");
//pps.store(new FileOutputStream(FILE_PATH), "注释");
pps.store(new FileWriter(FILE_PATH), "注释");
} catch (Exception e) {
e.printStackTrace();
}
}
}