随笔 - 17  文章 - 0  评论 - 0  阅读 - 1184

11-Properties基本介绍

11-Properties基本介绍

  1. Properties 类继承自 Hashtable 类并且实现了 Map 接口,也是使用一种键值对的形式来保存数据。(因为继承 Properties ,所以键值对不可以为 null )
  2. Properties 类使用特点和 Hashtable 类类似,但是请注意Properties 类的键值类型都为 String 。虽然可以直接调用父类 put 和 get 方法,但是 Properties 类本身是为了处理字符串键值对,所以调用 setProperty 和 getProperty 方法。
  3. Properties 类可以用于从以 .properties 为文件格式的文件,将其中的数据加载到 Properties 类对象,进行读取和修改,还能将该对象中存储的数据存储到以 .properties 为文件格式的文件。
  4. Properties 类内部还维护了一个 protected Properties defaults,这个 Properties 类对象是为了当本身的 Properties 类对象中查询不到对应 key 对应的 value 时,就会查询 protected Properties defaults,返回该设定的默认值。(这个 protected Properties defaults 在初始化时需指定)
  5. 将所需要的数据写在以 .properties 为文件格式的文件中,然后通过 Properties 类对象进行读取加载,可以便于数据的修改。只需修改文件,而不用修改代码。
  6. 实践练习
package map.properties;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesSource {

    @SuppressWarnings({"all"})
    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.setProperty("www.jingdong.com","192.168.0.1") ;
        properties.setProperty("www.baidu.com","192.168.0.2") ;
        properties.setProperty("www.douyin.com","192.168.0.3") ;

        System.out.println(properties.getProperty("www.baidu.com"));

        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<Object, Object> next =  iterator.next();
            String key = (String) next.getKey();
            String value = (String) next.getValue();

            System.out.println(key + value);
        }
    }
}

posted on   tenolk  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示