Properties

Properties属性集对象

其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用。

Properties核心作用

Properties代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中去。

属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value,后续做系统配置信息的。 

Properties的API

重点是Properties和IO流结合的方法

示例

存储Properties属性集的键值对数据到属性文件中去

/**
    目标:Properties的概述和使用(框架底层使用,了解这个技术即可)(保存数据到属性文件)

    Properties: 属性集对象。
         其实就是一个Map集合。也就是一个键值对集合,但是我们一般不会当集合使用,
         因为有HashMap。

    Properties核心作用:
         Properties代表的是一个属性文件,可以把键值对的数据存入到一个属性文件中去。
         属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value。

    大家在后期学的很多大型框架技术中,属性文件都是很重要的系统配置文件。
        users.properties
                admin=123456
                dlei=dlei

     需求:使用Properties对象生成一个属性文件,里面存入用户名和密码信息。

     Properties的方法:
         -- public Object setProperty(String key, String value) : 保存一对属性。  (put)
         -- public String getProperty(String key) : 使用此属性列表中指定的键搜索属性值 (get)
         -- public Set<String> stringPropertyNames() : 所有键的名称的集合  (keySet())
         -- public void store(OutputStream out, String comments): 保存数据到属性文件中去
         -- public void store(Writer fw, String comments): 保存数据到属性文件中去

     小结:
            Properties可以保存键值对数据到属性文件

 */
public class PropertiesDemo01 {
    public static void main(String[] args) throws Exception {
        // 需求:使用Properties把键值对信息存入到属性文件中去。
        Properties properties = new Properties();
        properties.setProperty("admin", "123456");
        properties.setProperty("dlei", "003197");
        properties.setProperty("heima", "itcast");
        System.out.println(properties);

        /**
           参数一:保存管道 字符输出流管道
           参数二:保存心得
         */
        properties.store(new FileWriter("io-app2/src/users.properties")
                , "this is users!! i am very happy! give me 100!");

    }
}

 加载属性文件中的数据到Properties对象中来

/**
    目标:Properties读取属性文件中的键值对信息。(读取)
    Properties的方法:
        -- public Object setProperty(String key, String value) : 保存一对属性。
        -- public String getProperty(String key) :使用此属性列表中指定的键搜索属性值
        -- public Set<String> stringPropertyNames() :所有键的名称的集合
        -- public void store(OutputStream out, String comments):保存数据到属性文件中去
        -- public synchronized void load(InputStream inStream):加载属性文件的数据到属性集对象中去
        -- public synchronized void load(Reader fr):加载属性文件的数据到属性集对象中去
    小结:
        属性集对象可以加载读取属性文件中的数据!!
 */
public class PropertiesDemo02 {
    public static void main(String[] args) throws Exception {
        // 需求:Properties读取属性文件中的键值对信息。(读取)
        Properties properties = new Properties();
        System.out.println(properties);

        // 加载属性文件中的键值对数据到属性对象properties中去
        properties.load(new FileReader("io-app2/src/users.properties"));

        System.out.println(properties);
        String rs = properties.getProperty("dlei");
        System.out.println(rs);
        String rs1 = properties.getProperty("admin");
        System.out.println(rs1);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值