Properties
Properties集合存储数据,遍历数据
public class ProPer {
public static void main(String[] args) {
method();
}
public static void method() {
Properties pro = new Properties();
pro.setProperty("小新","6");
pro.setProperty("妮妮","7");
pro.setProperty("风间","8");
Set<String> set = pro.stringPropertyNames();
for(String key : set) {
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
原来是命名问题导致报错。
store 方法
public class ProPer {
public static void main(String[] args) throws IOException {
method();
}
public static void method() throws IOException {
Properties pro = new Properties();
pro.setProperty("小新","6");
pro.setProperty("妮妮","7");
pro.setProperty("风间","8");
FileWriter fw = new FileWriter("H:\\test\\aa.txt");
pro.store(fw,"store data"); //不可以用中文,会乱码
fw.close();
}
}
load 方法
public class LoadDemo {
public static void main(String[] args) throws IOException {
method();
}
public static void method() throws IOException {
Properties pro = new Properties();
pro.load(new FileReader("H:\\test\\aa.txt"));
Set<String> strings = pro.stringPropertyNames();
for (String key : strings) {
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
}
}
}