首页 > 其他 > 详细

Properties和IO流结合的方法练习

时间:2020-04-25 02:18:09      阅读:67      评论:0      收藏:0      [点我收藏+]

技术分享图片

//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流结合的方法练习

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!