码迷,mamicode.com
首页 > 其他好文 > 详细

Properties和IO流结合的方法练习

时间:2020-04-25 01:24:13      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:ade   col   main   except   数据保存   sde   ring   img   demo   

技术图片

//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");
     fw.close(); 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);
     fr.close(); 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);
     fw.close(); } }

运行结果:

技术图片

Properties和IO流结合的方法练习

标签:ade   col   main   except   数据保存   sde   ring   img   demo   

原文地址:https://www.cnblogs.com/pxy-1999/p/12770970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!