Properties属性文件

  • 是一个Map集合,键值对集合
  • 核心作用:Properties是用来代表属性文件的,可以读写属性文件里的内容

使用Properties读取属性文件里的键值对数据

public static void main(String[] args) throws IOException {
        //1.创建Properties对象出来,空容器
        Properties properties = new Properties();
        System.out.println(properties);

        //2.开始加载属性文件中的键值对数据到Properties对象
        properties.load(new FileReader("D:\\java\\src\\user.properties"));
        System.out.println(properties);

        //3.根据键取值
        System.out.println(properties.getProperty("张三"));

        //4.遍历全部的键和值
        Set<String> keys = properties.stringPropertyNames();
        for (String key : keys) {
            String value = properties.getProperty(key);
            System.out.println(key + "---->" + value);
        }

    }

使用Properties把键值对数据写出到属性文件

public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        //1.存储一些键值对数据
        properties.setProperty("kk","123456");
        properties.setProperty("sq","minmin");
        properties.setProperty("ds","hahahha");

        //2.把Properties对象的键值对数据存入到属性文件
        properties.store(new FileWriter("src/user2.properties")
                , "i saved many users!");

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值