Java 之 Properties类 属性集


一、概述

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

  java.util.Properties 继承与 Hashtable,来表示一个持久的属性集。

  Properties 可保存在流中或从流中加载,Properties集合是一个唯一和IO流相结合的集合。

  它使用键值结构存储数据,属性列表中每个键及其对应值都是一个字符串。Properties集合是一个双列集合,key和value默认都是字符串

二、Properties 类

  1、构造方法

public Properties() :创建一个空的属性列表

  2、基本的存储方法

public Object setProperty(String key, String value) : 保存一对属性。
public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
public Set<String> stringPropertyNames() :所有键的名称的集合,其中该键及其对应值是字符串。

  Demo:

 1  public static void main(String[] args) throws IOException {  2         //创建Properties集合对象
 3         Properties prop = new Properties();  4         //使用setProperty往集合中添加数据,都是字符串
 5         prop.setProperty("张三","16");  6         prop.setProperty("李四","17");  7         prop.setProperty("王五","18");  8         //prop.put(1,true);  9 
10         //使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中
11         Set<String> set = prop.stringPropertyNames(); 12 
13         //遍历Set集合,取出Properties集合的每一个键
14         for (String key : set) { 15             //使用getProperty方法通过key获取value
16             String value = prop.getProperty(key); 17             System.out.println(key+"="+value); 18  } 19     

  3、与流相关的方法

    (1)使用 store 方法,存储数据

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

void store(OutputStream out, String comments)
void store(Writer writer, String comments)

        参数

        OutputStream out:字节输出流,不能写入中文

        Writer writer:字符输出流,可以写中文

        String comments:注释,用来解释说明保存的文件是做什么的,不能使用中文,会产生乱码,默认是 Unicode编码,一般使用“” 空字符串。

      使用步骤

        ① 创建 Properties 对象,添加数据

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

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

        ④ 释放资源。

      Demo:

 1 public static void main(String[] args) throws IOException {  2         //1.创建Properties集合对象,添加数据
 3         Properties prop = new Properties();  4         prop.setProperty("张三","16");  5         prop.setProperty("李四","17");  6         prop.setProperty("王五","18");  7 
 8         //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地  9         FileWriter fw = new FileWriter("E:\\prop.txt"); 10 
11         //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 12         prop.store(fw,"save data"); 13 
14         //4.释放资源 15         fw.close();
16 
17         //prop.store(new FileOutputStream("E:\\prop2.txt"),""); // 写入中文后乱码 18 }

 

    (2)使用 load 方法,读取数据

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

 void load(InputStream inStream)
 void load(Reader reader)

      参数

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

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

       使用步骤

        ① 创建 Properties 集合对象

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

        ③ 遍历Properties集合

       注意

        ① 存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)

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

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

       Demo:

 1 public static void main(String[] args) throws IOException {  2         //1.创建Properties集合对象
 3         Properties prop = new Properties();  4         //2.使用Properties集合对象中的方法load读取保存键值对的文件
 5         prop.load(new FileReader("E:\\prop.txt"));  6         //prop.load(new FileInputStream("E:\\prop.txt")); 获取乱码  7         //3.遍历Properties集合
 8         Set<String> set = prop.stringPropertyNames();  9         for (String key : set) { 10             String value = prop.getProperty(key); 11             System.out.println(key+"="+value); 12  } 13 }

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM