Properties格式文件的读写

首先,我们得了解什么是Properties文件。Properties是一种配置文件,它继承Hashtable并且实现了Map接口,所以Properties类的内部是以k-v键值对的方式存储数据的,而且实现了Map接口,因此,存储的数据是无序的。

Properties格式文件的读取:

 要读取Properties文件,首先要创建输入流,

其次创建Properties类的对象,再调用Properties类的load()方法,将Properties属性文件对应的文件输入流,加载至Properties集合对象中。

最后根据key键,打印出对应的值。

public class test03 {
     public static void main(String[] args) {
        //Properties格式文件的读取
         
         //创建输入流
         try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\KSWJJ\\aaa\\bbb\\ccc\\data.properties.txt"))) {
            Properties props = new Properties();
             
             props.load(bis); //将“输入流”加载至Properties集合对象中
             
             //根据key,获取value
             System.out.println(props.get("cd"));
        } catch (IOException e) {
            e.printStackTrace();
        }

Properties格式文件的写入:

1.创建Properties集合对象

2.使用"输出流",再调用Properties类的store()方法将Properties集合中的kv键值对写入Properties文件。

 //Properties格式文件的写入
        try {
            Properties prop = new Properties();
             prop.put("f1", "1234");
             prop.put("f2", "4567");
             prop.put("f3", "1454");
             prop.put("f4", "7894");
             //使用"输出流",将Properties集合中的kv键值对写入Properties文件
             try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\\\KSWJJ\\\\aaa\\\\bbb\\\\ccc\\\\demo.properties"))) {
                    prop.store(bos,"just do IT");
        } 
        }catch (Exception e1) {
            e1.printStackTrace();
        }

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值