package bull1715.Properties;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo1 {
public static void main(String[] args) {
Properties prop = new Properties();
prop.put("张三", "001");
prop.put("李四", "002");
prop.put("王五", "003");
Set<Object> s = prop.keySet();
for (Object object : s) {
Object value = prop.get(object);
System.out.println(object + "=" + value);
}
System.out.println("-------------------");
Set<Map.Entry<Object, Object>> entrys = prop.entrySet();
for (Entry<Object, Object> entry : entrys) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + "=" + value);
}
}
}
package bull1715.Properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
public class PropertiesDemo2 {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
}
private static void method3() throws IOException {
Properties prop = new Properties();
FileWriter fw = new FileWriter("PD2.txt");
prop.setProperty("张一", "0001");
prop.setProperty("李二", "0002");
prop.setProperty("王三", "0003");
prop.store(fw, "PropertiesDemo2");
fw.close();
}
private static void method2() throws FileNotFoundException, IOException {
Properties prop = new Properties();
FileReader fr = new FileReader("PD2.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
}
private static void method1() throws FileNotFoundException {
Properties prop = new Properties();
PrintWriter pw = new PrintWriter("PD2.txt");
prop.setProperty("张三", "001");
prop.setProperty("李四", "002");
prop.setProperty("王五", "003");
prop.list(pw);
pw.close();
}
}