Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。不仅可以用它在单独一行中指定用等号分隔的多个键-值对,还可以用XML 文件装载和保存这些键-值对。
因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但不建议使用这两个方法,因为它们允许调用者插入其键或值不是 String 的项。相反,应该使用 setProperty 方法。如果在“不安全”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。类似地,如果在“不安全”的 Properties 对象(即包含非 String 的键)上调用 propertyNames 或 list 方法,则该调用将失败。
此类是线程安全的:多个线程可以共享单个 Properties 对象而无需进行外部同步。
字段:protected Properties defaults;
一个属性列表,包含属性列表中所有未找到值的键的默认值。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
构造函数:
1、无参构造函数:
// 创建一个无默认值的空属性列表
public Properties() {
this(null);
}
2、有默认值参数的构造函数:
//创建一个带有指定默认值的空属性列表
public Properties(Properties defaults) {
this.defaults = defaults;
}
3、从本地获取属性文件(输入流InputStream),并打印键值对
public static void main(String[] args) {
Properties properties=new Properties();
try{
//从本地文件读取属性:键值对
//调用load(InputStream)时,读取文件时使用的默认编码为ISO-8859-1,需要进行格式转换
FileInputStream fis=new FileInputStream(new File("D:\\testJAVAAPI\\test.properties"));
//从输入流中读取属性列表(键值对),保存到propertoes中
//void load(InputStream inStream):从输入流中读取属性列表(键和元素对)。
properties.load(fis);
//获得属性值
//String getProperty(String key) :用指定的键在此属性列表中搜索属性
//格式转换
String name=new String(properties.getProperty("name").getBytes("ISO-8859-1"),"UTF-8");
String pwd=properties.getProperty("pwd");
System.out.println("name="+name+" pwd="+pwd);
}catch (Exception e){
e.printStackTrace();
}
}
//结果:
name=我等着 pwd=123456789
4、将属性列表输出到其他文件流中(输出流OutputStream):
FileOutputStream fos=new FileOutputStream(new File("F:\\learnTest\\think in java\\propertiesTest\\learn.properties"));
//保存属性到本地文件,若是中文会出现乱码
//void store(OutputStream out, String comments)
//该方法适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
properties.store(fos,new String("注解".getBytes("UTF-8"),"ISO-8859-1"));
//结果:
#注解
#Sat Jun 17 19:17:39 CST 2017
name=wdz
pwd=123456
5、利用字符输入流(Reader)从本地获取属性。
public static void main(String[] args) {
Properties properties=new Properties();
try{
FileInputStream fis=new FileInputStream(new File("F:\\learnTest\\think in java\\propertiesTest\\test.properties"));
//读取文件时指定字符编码
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
BufferedReader reader=new BufferedReader(isr);
//void load(Reader reader) :按简单的面向行的格式从输入字符流中读取属性列表(键值对)
properties.load(reader);
//不用进行中文转换
String name=properties.getProperty("name");
String pwd=properties.getProperty("pwd");
System.out.println("name="+name+" pwd="+pwd);
}catch (Exception e){
e.printStackTrace();
}
}
6、利用字符输出流保存到本地
FileOutputStream fos=new FileOutputStream(new File("F:\\learnTest\\think in java\\propertiesTest\\learn.properties"));
OutputStreamWriter ost=new OutputStreamWriter(fos,"UTF-8");
BufferedWriter writer=new BufferedWriter(ost);
//void store(Writer writer, String comments):适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符。
//虽然键值对可以输出中文,但是中文注解会出现乱码
properties.store(writer,"comment");
//结果
#comment
#Sat Jun 17 19:45:41 CST 2017
name=文档中
pwd=123456
7、也可以读取和保存XML文档的键值对
public static void main(String[] args) {
Properties properties=new Properties();
try{
FileInputStream fis=new FileInputStream(new File("F:\\learnTest\\think in java\\propertiesTest\\test.xml"));
//没有loadFromXML(reader)方法
// properties.loadFromXML(reader);
properties.loadFromXML(fis);
//中文也不会出现乱码
String name=properties.getProperty("name");
String pwd=properties.getProperty("pwd");
FileOutputStream fos=new FileOutputStream(new File("F:\\learnTest\\think in java\\propertiesTest\\learn.xml"));
properties.setProperty("name","文档中");
properties.setProperty("pwd","123456");
//可以设置编码格式
properties.storeToXML(fos,"comment","UTF-8");
}catch (Exception e){
e.printStackTrace();
}
}
//结果:(注意XML文件的正确格式)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>comment</comment>
<entry key="name">文档中</entry>
<entry key="pwd">123456</entry>
</properties>
8、读取程序中的配置文件属性
public static void main(String[] args) {
Properties properties=new Properties();
InputStream is=null;
try{
//文件位置:resources/test.properties
is=Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties");
properties.load(is);
//出现中文乱码
String name=properties.getProperty("name");
}catch (Exception e){
e.printStackTrace();
}
}
参考文章:
http://www.apihome.cn/api/java/Properties.html
http://blog.youkuaiyun.com/lmb55/article/details/50769117