00096_Properties类

1、Properties类介绍

  (1)Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串;

  (2)特点

  Hashtable的子类,map集合中的方法都可以用;

  该集合没有泛型。键值都是字符串;

  它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备;

  有和流技术相结合的方法。

 1 import java.util.Properties;
 2 import java.util.Set;
 3 
 4 /*
 5  * 
 6  * Properties集合,它是唯一一个能与IO流交互的集合
 7  * 
 8  * 需求:向Properties集合中添加元素,并遍历
 9  * 
10  * 方法:
11  * public Object setProperty(String key, String value)调用 Hashtable 的方法 put。
12  * public Set<String> stringPropertyNames()返回此属性列表中的键集,
13  * public String getProperty(String key)用指定的键在此属性列表中搜索属性
14  */
15 public class PropertiesDemo01 {
16     public static void main(String[] args) {
17         // 创建集合对象
18         Properties prop = new Properties();
19         // 添加元素到集合
20         // prop.put(key, value);
21         prop.setProperty("a", "a1");
22         prop.setProperty("b", "b1");
23         prop.setProperty("c", "c1");
24 
25         // System.out.println(prop);//测试的使用
26         // 遍历集合
27         Set<String> keys = prop.stringPropertyNames();
28         for (String key : keys) {
29             // 通过键 找值
30             // prop.get(key)
31             String value = prop.getProperty(key);
32             System.out.println(key + "==" + value);
33         }
34     }
35 }

2、将集合中内容存储到文件

  (1)需求:使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作;

  (2)分析:

  创建Properties集合;

  添加元素到集合;

  创建流;

  把集合中的数据存储到流所对应的文件中。

 1 import java.io.FileWriter;
 2 import java.io.IOException;
 3 import java.util.Properties;
 4 
 5 public class PropertiesDemo02 {
 6     public static void main(String[] args) throws IOException {
 7         // 1,创建Properties集合
 8         Properties prop = new Properties();
 9         // 2,添加元素到集合
10         prop.setProperty("a", "a1");
11         prop.setProperty("b", "b1");
12         prop.setProperty("c", "c1");
13 
14         // 3,创建流
15         FileWriter out = new FileWriter("d:\Java\prop.properties");
16         // 4,把集合中的数据存储到流所对应的文件中
17         prop.store(out, "save data");
18         // 5,关闭流
19         out.close();
20     }
21 }

  运行结果:在d盘根目录下生成prop.properties

  

3、读取文件中的数据,并保存到集合

  (1)需求:从属性集文件prop.properties 中取出数据,保存到集合中
  (2)分析:
  创建集合
  创建流对象
  把流所对应文件中的数据 读取到集合中
    load(InputStream)  把指定流所对应的文件中的数据,读取出来,保存到Propertie集合中
    load(Reader)  
  关闭流
  显示集合中的数据

 1 import java.io.FileInputStream;
 2 import java.io.IOException;
 3 import java.util.Properties;
 4 
 5 public class PropertiesDemo03 {
 6     public static void main(String[] args) throws IOException {
 7         // 1,创建集合
 8         Properties prop = new Properties();
 9         // 2,创建流对象
10         FileInputStream in = new FileInputStream("d:\Java\prop.properties");
11         // FileReader in = new FileReader("prop.properties");
12         // 3,把流所对应文件中的数据 读取到集合中
13         prop.load(in);
14         // 4,关闭流
15         in.close();
16         // 5,显示集合中的数据
17         System.out.println(prop);
18 
19     }
20 }

  运行结果:
  

  注意:使用字符流FileReader就可以完成文件中的中文读取操作了

原文地址:https://www.cnblogs.com/gzdlh/p/8097391.html