代码如下:
public class MyWork { public static void main(String[] args) throws IOException { Properties pro1 = new Properties(); pro1.setProperty("0001","张三"); pro1.setProperty("0002","李四"); pro1.setProperty("0003","王武"); //写出 File f = new File("testProperties/testObject.txt"); FileWriter fw = new FileWriter(f); pro1.store(fw,null); fw.close(); Properties pro2 = new Properties(); FileReader fr = new FileReader(f); pro2.load(fr); fr.close(); //读入 Set<String> keys = pro2.stringPropertyNames(); Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key = it.next(); String value = pro2.getProperty(key); System.out.println(key + "======" + value); } //for (String key : keys) { // String value = pro2.getProperty(key); // System.out.println(key + "======" + value); //} } }