Java中Propertise属性集合详细总结,它继承Hashtable,使用键值存储数据

1.对Propertises属性集合的创建,保存一对键值,根据键搜索值,获取所有键名称的集合,然后就是遍历获取所有的键值对的总结。

private static void demo1() {
        Properties po=new Properties();
        //保存键值对
        po.setProperty("照照","22");
        po.setProperty("丽丽","23");
        po.setProperty("佳佳","23");
       //遍历键值对
        Set<String> set=po.stringPropertyNames(); //获取所有键的集合
        for (String key:set){
            String value=po.getProperty(key); //获取键对应的值
            System.out.println(key+"="+value);
        }

    }

 

2.Propertise属性集合中的store方法,把集合中中的数据持久化的写到硬盘中存储。

    private static void demo2() throws IOException {
        Properties po=new Properties();
        //保存键值对
        po.setProperty("照照","22");
        po.setProperty("丽丽","23");
        po.setProperty("佳佳","23");
        //创建字节/字符输出流的对象,构造方法中绑定要输出的目的地
        FileWriter fw=new FileWriter("day_04_code\\ab.txt");
        //使用store方法,把集合中的方法持久化的写入到硬盘中
        po.store(fw,"save data");
        fw.close();
        
    }

3.Propertise属性集合中的load方法,把硬盘中保存的文件读取到集合中使用。

  private static void demo3() throws IOException {
       //创建集合
        Properties po=new Properties();
        //可以使用字节输入流或者字符输入流读取文件,但是我们一般使用字符输入流
        FileReader fr=new FileReader("day_04_code\\ab.txt");
        //把文件中的数据读取到集合中
        po.load(fr);
    //已经读取成功了,我们可以遍历一下集合看下
        Set<String> set = po.stringPropertyNames();
        for (String key:set){
            String value=po.getProperty(key);
            System.out.println(key+"="+value);
        }
    }

 

4.如果对你有帮助,希望可以点个赞呦,如果热爱Java和算法,可以关注落花一叶知秋博主,博主会跟大家一起成长。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值