属性集合类

来源:互联网 发布:飞猪是淘宝的吗 编辑:程序博客网 时间:2024/06/08 00:17

Properties:属性集合类(继承自Hashtable

public void load(Reader reader);//将文件中的数据加载到属性集合中public void store(Writer writer,String comments);//将属性集合中的数据保存到文件中

代码实现如下:

package _13.homework;import java.util.Properties;import java.util.Set;public class Demo1 {    public static void main(String[] args) {        Properties prop = new Properties();        prop.put("祝英台", "梁山伯");        prop.put("朱丽叶", "罗密欧");        prop.put("王宝钏", "薛平贵");        prop.put("杨玉环", "唐玄宗");        Set<Object> keySet = prop.keySet();        for (Object set : keySet) {            Object value = prop.get(set);            System.out.println(set + "=" + value);        }    }}
package _13.homework;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Properties;public class Demo1 {    public static void main(String[] args) throws IOException {        store();        load() ;    }    private static void load() throws IOException {        Properties prop = new Properties() ;        FileReader fr = new FileReader("name.txt") ;        prop.load(fr) ;        fr.close() ;        System.out.println(prop);    }    private static void store() throws IOException {        Properties prop = new Properties();        prop.setProperty("祝英台", "梁山伯") ;        prop.setProperty("朱丽叶", "罗密欧") ;        prop.setProperty("王宝钏", "薛平贵") ;        FileWriter fw = new FileWriter("F:\\a,txt") ;        prop.store(fw, "names content") ;        fw.close() ;    }}