基本介绍
- Properties 类继承自 Hashtable 类并且实现了 Map 接口,也是使用一种键值对的形式来保存数据。
- 他的使用特点和Hashtable类似。
- Properties 还可以用于从xxx.properties 文件中,加载数据到Properties类对象,并进行读取和修改。
- 说明:工作后 xxx.properties文件通常作为配置文件,这个知识点在IO流举例
https://www.cnblogs.com/xudong-bupt/p/3758136.htmlhttps://www.cnblogs.com/xudong-bupt/p/3758136.html
基本使用实例
public class Properties_ {
public static void main(String[] args) {
//1. Properties 继承Hashtable
//2.可以通过k-v存放数据,当然key和 value不能为null
Properties properties = new Properties();
//增
properties.put("john", 100); //添加 K-V
// properties.put(null , 100); //抛出空指针异常
//properties.put("john", null); //抛出空指针异常
properties.put("lucy", 100); //添加 K-V
properties.put("lic", 100); //添加 K-V
properties.put("lic",88); //替换value
System.out.println(properties);
//删除
properties.remove("lic");
//改
properties.put("john","北京大学");
System.out.println(properties);
//查
System.out.println(properties.get("john"));
System.out.println(properties.getProperty("john"));
}
}
输出:
{john=100, lic=88, lucy=100}
{john=北京大学, lucy=100}
北京大学
北京大学