一、概述
java.util.Properties
继承于Hashtable
,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。
Hashtable
是最早期的双列集合,但由于其单线程操作被Hashmap
替代了,然而其子类Properties
仍活跃着,因为是唯一和IO流相结合的集合。Properties
类的出现简化了对属性持久化的操作。属性持久化又大大简化了建立高扩展性程序的过程。
简单理解:Properties
类是一个可以持久化读写数据(键值对)的集合对象。
知识点连接:持久化作用是将程序数据在持久状态和瞬时状态间转换的机制。即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘)。持久化的主要应用是将内存中的对象存储在关系型的数据库中,当然也可以存储在磁盘文件中、XML数据文件中等等。
二、Properties类
2.1 构造方法
public Properties()
:创建一个空的属性列表。Properties(Properties defaults)
: 创建一个带有指定默认值的空属性列表。
2.2 基本的存储方法
-
使用
Properties
集合存储数据,遍历取出Properties
集合中的数据。Properties
集合是一个双列集合,key
和value
默认都是字符串 -
Properties
集合有一些操作字符串的特有方法public Object setProperty(String key, String value)
: 调用Hashtable
的方法 put,保存一对属性。public String getProperty(String key)
:通过key找到value值,此方法相当于Map
集合中的get(key)
方法public Set<String> stringPropertyNames()
:返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map
集合中的keySet
方法
public class Demo01Properties { public static void main(String[] args) throws IOException { //创建Properties集合对象 Properties prop = new Properties(); //使用setProperty往集合中添加数据 prop.setProperty("赵丽颖","168"); prop.setProperty("迪丽热巴","165"); prop.setProperty("古力娜扎","160"); //prop.put(1,true); //使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中 Set<String> set = prop.stringPropertyNames(); //遍历Set集合,取出Properties集合的每一个键 for (String key : set) { //使用getProperty方法通过key获取value String value = prop.getProperty(key); System.out.println(key+"="+value); } } } 输出结果: 赵丽颖=168 迪丽热巴=165 古力娜扎=160
2.3 与流相关的方法
store 方法
Properties
集合中的方法 store
,可以把集合中的临时数据,持久化写入到硬盘中存储
-
public void store(OutputStream out, String comments)
-
public void store(Writer writer, String comments)
-
参数 :
OutputStream out
: 字节输出流,不能写入中文
Writer writer
: 字符输出流,可以写中文
String comments
:注释,用来解释说明保存的文件是做什么用的注意:
String comments
不能使用中文,会产生乱码,默认是Unicode编码
一般使用""
空字符串
-
-
使用步骤:
- 创建
Properties
集合对象,添加数据 - 创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
- 使用
Properties
集合中的方法store
,把集合中的临时数据,持久化写入到硬盘中存储 - 释放资源
- 创建
-
代码演示:
public class Demo02Properties { public static void main(String[] args) throws IOException { //1.创建Properties集合对象,添加数据 Properties prop = new Properties(); prop.setProperty("赵丽颖","168"); prop.setProperty("迪丽热巴","165"); prop.setProperty("古力娜扎","160"); //2.创建字符输出流对象,构造方法中绑定要输出的目的地 FileWriter fw = new FileWriter("prop.txt"); //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储 prop.store(fw,"save data"); //4.释放资源 fw.close(); //创建字节输出流对象,构造方法中绑定要输出的目的地 prop.store(new FileOutputStream("prop2.txt"),""); //一堆乱码 } }
load 方法
Properties
集合中的方法 load
,可以把硬盘中保存的文件(键值对),读取到集合中使用
-
public void load(InputStream inStream)
:从字节输入流中读取键值对。 -
public void load(Reader reader)
:从字符输入流中读取键值对。- 参数 :
InputStream inStream
: 字节输入流,不能读取含有中文的键值对
Reader reader
:字符输入流,能读取含有中文的键值对
- 参数 :
-
使用步骤 :
-
创建
Properties
集合对象 -
使用
Properties
集合对象中的方法load
,读取保存键值对的文件 -
遍历
Properties
集合注意:
- 文本中的数据,必须是键值对形式
- 存储键值对的文件中,键与值默认的连接符号可以使用 空格、等号、冒号等符号分隔
- 存储键值对的文件中,可以使用
#
进行注释,被注释的键值对不会再被读取 - 存储键值对的文件中,键与值默认都是字符串,不用再加引号
-
-
代码演示 :
public class Demo03Properties { public static void main(String[] args) throws IOException { //1.创建Properties集合对象 Properties prop = new Properties(); //2.使用Properties集合对象中的方法load读取保存键值对的文件 prop.load(new FileReader("prop.txt")); //prop.load(new FileInputStream("prop.txt")); //使用stringPropertyNames把Properties集合中的键取出,存储到一个Set集合中 Set<String> set = prop.stringPropertyNames(); //3.遍历Properties集合 for (String key : set) { //使用getProperty方法通过key获取value String value = prop.getProperty(key); System.out.println(key+"="+value); } } }