Properties

可以和流相关联的集合对象Properties.
Map
|–Hashtable
|–Properties
Properties:该集合不需要泛型,因为该集合中的键值对都是String类型。

        Properties prop = System.getProperties();
        // prop.list(System.out);// 目的是控制台。
        // 需求是:将jvm的属性信息存储到一个文件中。
        prop.list(new PrintStream("java.txt"));


 Properties prop = System.getProperties();
        Set<String> keys = prop.stringPropertyNames();
        for (String key : keys) {
    System.out.println(key + ":" + prop.getProperty(key));
        }



// 1,创建Properties(Map)对象
        Properties prop = new Properties();
        // 2.使用流加载配置文件。
        FileInputStream fis = new FileInputStream("c:\\qq.txt");
        // 3。使用Properties 对象的load方法将流中数据加载到集合中。
        prop.load(fis);
        // 遍历该集合
        Set<Entry<Object, Object>> entrySet = prop.entrySet();
        Iterator<Entry<Object, Object>> it = entrySet.iterator();
        while (it.hasNext()) {
            Entry<Object, Object> next = it.next();
            Object key = next.getKey();
            Object value = next.getValue();
        }
        // 通过键获取指定的值
        Object object = prop.get("jack");
        System.out.println(object);
        // 通过键修改值
        prop.setProperty("jack", "888888");
        // 将集合中的数据写入到配置文件中。
        FileOutputStream fos = new FileOutputStream("c:\\qq.txt");
        // 注释:
        prop.store(fos, "yes,qq");
        fos.close();
        fis.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值