Java基础理论之属性集

Properties类

构造方法

  • public Properties() :创建一个空的属性列表。

基本的存储方法

  • public Object setProperty(String key, String value) : 保存一对属性。
  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
  • public Set<String> stringPropertyNames() :所有键的名称的集合。

与流相关的方法

  • public void load(InputStream inStream): 从字节输入流中读取键值对。

参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了
小贴士:文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。

举个栗子:

 public static void testProperties(){
        Properties properties = new Properties();
        properties.setProperty("key","value");
        System.out.println(properties);
        properties.setProperty("key2","value2");
        properties.forEach((key,value)->{
            System.out.println("key:"+ key);
            System.out.println("value:"+ value);
        });
        properties.setProperty("key3","value3");
        properties.setProperty("key3","value3");
        Set<String> keys = properties.stringPropertyNames();
        keys.forEach(System.out::println);
        File file = new File("C:\\Users\\admin\\Desktop\\test.txt");
        try(FileInputStream fileInputStream = new FileInputStream(file);){
            properties.load(fileInputStream);
            System.out.println(properties);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值