ProPerties文档的使用

Properties

1. 什么是properties?

​ 它是一种文本格式。里面的数据有自己规定的格式,一般存放键值对类型的数据。

好处:可以很好很方便的跟java程序进行交互。java提供专门跟properties交互的API。存到properties文件中的内容是可以持久化的。

2.properties的使用
  • 里面的注释是使用#
  • 键值之间一般使用=分割。也可以使用:号跟空格分割,但是尽量使用=号。
3. JAVA跟properties的交互
//        1, java   --- IO ---  硬盘上的文件
        File file = new File("day04-02/src/hello.properties");
        System.out.println(file.exists());
        FileInputStream fis = new FileInputStream(file);
//        2, java 已经帮我们定义好了Properties类
        Properties p  = new Properties();
        p.load(fis);

//        枚举器(Hashtable  Vector) 迭代器
        Enumeration<Object> keys = p.keys();
        while (keys.hasMoreElements()){
            String key = (String) keys.nextElement();
            System.out.println(key + " = " + p.getProperty(key) );
        }
3.2 数据的存储
//        1,创建我们要保存的文件,如果不存在就创建这个文件
    File f = new File("day04-02/src/hello2.properties");
    if(!f.exists()){
        f.createNewFile();
    }
        FileOutputStream fos = new FileOutputStream(f);


//    准备要保存的数据集合
    Properties p = new Properties();
    p.setProperty("name","张三");
    p.setProperty("age","18");
    p.setProperty("sex","男");

//    将集合中的数据直接存储到文件中
    p.store(fos,"备注随便打打");
3.3 其它

获取资源文件的流我们还可以通过
InputStream fis = Demo03.class.getClassLoader().getResourceAsStream("hello.properties");方式。但是这种方式会将数据缓存到虚拟机中,如果缓存有数据则使用,没有则读取文件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值