File类与常用IO流第七章——Properties集合

时间:2023-03-09 17:27:35
File类与常用IO流第七章——Properties集合
  • Properties概述

java.util.Properties extends Hashtable<k,v> implements Map<k,v>

Properties类表示了一个持久的属性集。

Properties可以保存在流中或从流中加载。

Properties集合是唯一一个和IO流相结合的集合:

可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储。

可以使用Properties集合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用。

属性列表中每个键值对及其对应值都是一个字符串。

Properties集合是一个双列集合,key和value默认都是字符串。

  • 使用Properties集合存储数据,遍历取出Properties集合中的数据

Properties集合是一个Map集合,key和value默认都是字符串。

Properties集合中有一些操作字符串的特有方法

  • Object setProperty(String key,String value):调用Hashtable的方法put。

  • String getProperty(String key):用指定的键(key)在此属性列表中搜索属性(value)。相当于Map集合中的get(key)方法。

  • Set<String> stringPropertyNames():返回此属性列表中的键值,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。此方法相当于Map集合中的keySet方法。

 1 Properties prop = new Properties();
2 prop.setProperty("一班","68人");
3 prop.setProperty("二班","78人");
4 prop.setProperty("三班","88人");
5 //prop.put(1,true);//并没有被保存进去
6
7 Set<String> stringSet = prop.stringPropertyNames();
8
9 for (String key : stringSet) {
10 String value = prop.getProperty(key);
11 System.out.println(key+"="+value);
12 }

运行结果:prop.put(1,true);//并没有被保存进去

  • Properties集合中的方法store

使用这个方法可以将集合中的临时数据,持久化写入到硬盘中存储

  1. void store(OutputStream out , String comments)

  2. void store(Write write , String comments)

  3. 参数:

    1. OutpueStream out :字节输出流,不能写中文

    2. Write write :字符输出流,可以写中文

    3. String comments :注释,用来解释说明保存的文件是做什么用的

      1. comments的注意:不能使用中文,会产生乱码,默认Unicode编码格式。一般使用空字符串 " " ;1

  4. 使用步骤:

    1. 创建Properties集合,添加数据

    2. 创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地

    3. 使用Properties集合中的方法store将集合中的临时数据,持久化写入到硬盘中存储

    4. 释放资源

      1. 匿名对象使用完毕会自动关闭其中申请的资源。

 1 //1.创建Properties集合对象,添加数据
2 Properties prop = new Properties();
3 prop.setProperty("赵丽颖","168");
4 prop.setProperty("迪丽热巴","165");
5 prop.setProperty("古力娜扎","160");
6
7 //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
8 FileWriter fw = new FileWriter("09_IOAndProperties\\prop.txt");
9
10 //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
11 prop.store(fw,"save data");
12
13 //4.释放资源
14 fw.close();

1 //1.创建Properties集合对象,添加数据
2 Properties prop = new Properties();
3 prop.setProperty("赵丽颖","168");
4 prop.setProperty("迪丽热巴","165");
5 prop.setProperty("古力娜扎","160");
6
7 //2.&3.&4.匿名对象使用完会自动关闭其中申请的资源
8 prop.store(new FileWriter("09_IOAndProperties\\prop2.txt"),"");
  • Properties集合中的方法load

使用此方法,可以将硬盘中保存的文件(键值对),读取到集合中使用

  1. void load(InputStream inStream)

  2. void load(Reader reader)

  3. 参数:

    1. InputStream inStream:字节输入流,不能读取含有中文的键值对

    2. Reader reader:字符输入流,能读取含有中文的键值对

  4. 使用步骤:

    1. 创建Properties集合对象

    2. 使用Properties集合对象中的方法load读取保存键值对的文件

    3. 遍历Properties集合

  5. 注意:

    1. 存储键值对的文件中,键与值默认的连接符号可以使用 = 或空格

    2. 存储键值对的文件中,可以使用 # 进行注释,被注释的键值对不会再被读取

    3. 存储荐椎对的文件中,键与值的默认都是字符串,不用再加引号

  6. 代码:

 1 //1.创建Properties集合对象
2 Properties prop = new Properties();
3 //2.使用Properties集合对象中的方法load读取保存键值对的文件
4 prop.load(new FileReader("09_IOAndProperties\\prop.txt"));
5 //prop.load(new FileInputStream("09_IOAndProperties\\prop.txt"));
6 //3.遍历Properties集合
7 Set<String> set = prop.stringPropertyNames();
8 for (String key : set) {
9 String value = prop.getProperty(key);
10 System.out.println(key+"="+value);
11 }