属性集 Properties
一般用于配置文件的读取,很少用于传输,传输的话,可能会使用json或者流
store
package com.yu.properties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo02Properties {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.setProperty("赵丽颖", "168");
prop.setProperty("迪丽热巴", "165");
prop.setProperty("古力娜扎", "160");
/* //字符流可以写入中文
FileWriter fw = new FileWriter("D:\\123\\a.txt",false);
prop.store(fw,"name:Jason");
fw.close();*/
//字节流写入中文时会出现乱码
prop.store(new FileOutputStream("D:\\123\\a2.txt", true), "name:Jason");
}
}
load
package com.yu.properties;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Demo03PropertiesLoad {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
//可以读取中文
prop.load(new FileReader("D:\\123\\a.txt"));
//不可以读取中文
//prop.load(new FileInputStream("D:\\123\\a.txt"));
Set<String> set = prop.stringPropertyNames();
for (String k : set) {
String v = prop.getProperty(k);
System.out.println(k + " " + v);
}
}
}