Properties继承自Hashtable,也是map集合的一种,具有Map集合的常用方法,但是Properties的键值对都是String类型,Properties可以操作properties文件,因此,Properties集合和properties文件经常搭配使用

准备工作

准备一个user.properties空文件

image-20220906082209030

准备一个user.xml文件,有方法要使用到

image-20220906115810207

构造器方法

  1. public Properties()

    默认构造

  2. public Properties(Properties defaults)

    传入一个Properties对象,构造出来的Properties对象和传入的Properties对象完全一样,包括在虚拟机上的内存地址,属于浅拷贝

1
2
// 构造1
Properties properties = new Properties();
1
2
3
4
// 构造2
Properties properties1 = new Properties();
// 传入properties完成构造
Properties properties2 = new Properties(properties1);

setProperty

添加键值对到Properties集合中去,这个方法是线程安全的

public synchronized Object setProperty(String key, String value)

1
2
3
4
Properties properties = new Properties();
// 添加键值对到Properties集合中去
properties.setProperty("name", "jack");
properties.setProperty("age", "18");

getProperty

从Properties集合中取出键对应的值,有两种重载形式

  1. public String getProperty(String key)

    如果该键key在Properties集合中不存在,取出的值就是null

  2. public String getProperty(String key, String defaultValue)

​ 如果该键key在Properties集合中不存在,取出的值就是defaultValue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
// hobby键不存在,返回null
System.out.println(properties.getProperty("hobby"));
// hobby键不存在,返回默认值"rap"
System.out.println(properties.getProperty("hobby", "rap"));
// 输出:
// jack
// 18
// null
// rap

list

将数据显示到指定设备

有两种重载形式

  1. public void list(PrintStream out)

    将数据传输到PrintStream及其子类中去

  2. public void list(PrintWriter out)

    将数据传输到PrintWriter及其子类中去

1
2
3
4
5
6
7
8
9
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
// 将所有Properties集合中的键值对显示到终端
properties.list(System.out);
// 输出结果:
// -- listing properties --
// age=18
// name=jack

propertyNames

返回Properties集合类中所有的key的枚举Enumeration

public Enumeration<?> propertyNames()

1
2
3
4
5
6
7
8
9
10
11
12
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
// 获得properties的所有key
Enumeration<?> keys = properties.propertyNames();
// 遍历Enumeration集合
while (keys.hasMoreElements()) {
System.out.println(keys.nextElement());
}
// 结果
// age
// name

stringPropertyNames

返回Properties集合类中所有的key的Set<String>集合

public Set<String> stringPropertyNames()

1
2
3
4
5
6
7
8
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
// 获得key的Set<String>集合
Set<String> keys = properties.stringPropertyNames();
System.out.println(keys);
// 输出结果:
// [age, name]

store

将Properties集合中的键值对存到properties配置文件中,原来文件的内容会覆盖还是追加取决于输出流的输出方式

有两种重载形式

  1. public void store(Writer writer, String comments)

    传入Writer对象或其子类对象,comments表示添加对应的注释到配置文件中

  2. public void store(OutputStream out, String comments)

​ 传入Writer对象或其子类对象,comments表示添加对应的注释到配置文件中

1
2
3
4
5
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
// 保存到配置文件
properties.store(new FileWriter("E://user.properties"), "storetofiles");

结果

image-20220906114713361

load

加载properties配置文件,将配置文件的所有键值对存放到Properties集合中

有两种重载形式

  1. public synchronized void load(Reader reader) throws IOException

    这个传入的是Reader对象或其子类对象

  2. public synchronized void load(InputStream inStream) throws IOException

​ 这个传入的是InputStream对象或其子类对象

1
2
3
4
5
6
7
8
9
Properties properties = new Properties();
// 加载user.properties配置文件
properties.load(new FileReader("E://user.properties"));
// 取值
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
// 结果:
// jack
// 18

storeToXML

将Properties集合转存为XML文件,有两种重载形式

  1. public void storeToXML(OutputStream os, String comment)

    使用默认编码格式UTF-8存储

  2. public void storeToXML(OutputStream os, String comment, String encoding)

​ 指定编码格式存储

1
2
3
4
5
Properties properties = new Properties();
properties.setProperty("name", "jack");
properties.setProperty("age", "18");
// 将Properties集合转存为XML,无注释,指定字符编码为UTF-8
properties.storeToXML(Files.newOutputStream(Paths.get("E:\\user.xml")), null, "UTF-8");

结果

image-20220906115920446`

loadFromXML

加载XML配置文件中的键值对到Properties集合中

public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException

Properties properties = new Properties();
// 加载user.xml配置文件
properties.loadFromXML(Files.newInputStream(Paths.get("E:\\user.xml")));
// 取出属性
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
// 输出结果:
// jack
// 18