Properties和IO流结合的方法练习

//void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中
public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException{
        //把集合中的数据保存到文件
        myStore();
    }

    private static void myStore() throws IOException {
        //void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中
        Properties prop = new Properties();
        prop.setProperty("001","设置第1个元素");
        prop.setProperty("002","设置第2个元素");
        prop.setProperty("003","设置第3个元素");

        FileWriter fw = new FileWriter("myFile\\fw.txt");

        prop.store(fw,null);
    }
}

运行结果:

//void load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。
public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException{
        //把集合中的数据保存到文件
//        myStore();

        //把文件中的数据保存到集合
        //void load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。
        myLode();
    }

    private static void myLode() throws IOException{
        //void load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。
        FileReader fr = new FileReader("myFile\\fw.txt");

        Properties prop = new Properties();
        prop.load(fr);

        System.out.println(prop);
    }

    private static void myStore() throws IOException {
        //void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中
        Properties prop = new Properties();
        prop.setProperty("001","设置第1个元素");
        prop.setProperty("002","设置第2个元素");
        prop.setProperty("003","设置第3个元素");

        FileWriter fw = new FileWriter("myFile\\fw.txt");

        prop.store(fw,null);
    }
}

运行结果:

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/12770970.html