java.util.Properties 繼承了 java.util.Hashtable<Object, Object>類
基本用法是new Properties()再調(diào)用load方法讀取相應(yīng)的配置文件
package com.demon.test.testProperties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class Demo {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties properties = new Properties();
// properties.load(inStream); //調(diào)用load(InputStream inStream);//從流中讀入配置文件
// properties.load(reader); //調(diào)用load(Reader reader);//從流中讀入配置文件
// properties.loadFromXML(in); //調(diào)用loadFromXML(InputStream in)//從流中讀入xml格式配置文件
// properties.store(out, comments); //調(diào)用store(OutputStream out, String comments)//輸出配置文件到流
properties.load(new FileInputStream("properties_InputStream"));
properties.store(System.out, "show this properties 1");
properties.load(new FileReader("properties_Reader"));
properties.store(System.out, "show this properties 2");
properties.loadFromXML(new FileInputStream("XML_properties_InputStream"));
properties.store(System.out, "show this properties 3");
}
}
輸出如下:
#show this properties 1
#Mon Dec 25 17:52:12 CST 2017
name=demon
#show this properties 2
#Mon Dec 25 17:52:12 CST 2017
age=123
name=demon
#show this properties 3
#Mon Dec 25 17:52:12 CST 2017
age=123
name=demon.li
key=value
讀入的三個(gè)配置文件內(nèi)容為:
properties_InputStream:
name = demon
properties_Reader
name = demon
age = 123
XML_properties_InputStream
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="name">demon.li</entry>
<entry key="key">value</entry>
</properties>
1.輸入
Properties 從流中讀入配置時(shí)調(diào)用的三個(gè)方法:
public synchronized void load(Reader reader) throws IOException {
load0(new LineReader(reader));
}
public synchronized void load(InputStream inStream) throws IOException {
load0(new LineReader(inStream));
}
public synchronized void loadFromXML(InputStream in)
throws IOException, InvalidPropertiesFormatException
{
XmlSupport.load(this, Objects.requireNonNull(in));
in.close();
}
可以看到,load(Reader reader)、load(InputStream inStream)是為load0 (LineReader lr)方法做了代理,分析load(InputStream inStream)方法和Properties$LineReader內(nèi)部類,可以看到,load0 (LineReader lr)調(diào)用了lr.readLine()方法從流中解析出一行有效的配置,再解析成對(duì)應(yīng)的key和value,調(diào)用Object java.util.Hashtable.put(Object key, Object value)將解析出來的key-value添加到自身(Hashtable)中
//java.util.Properties.load0(LineReader)
String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
put(key, value);
loadFromXML(InputStream in)方法調(diào)用了java.util.Properties.XmlSupport.load(Properties, InputStream)最終調(diào)用的是sun.util.spi.XmlPropertiesProvider.load(Properties arg0, InputStream arg1)方法,相對(duì)復(fù)雜但是從使用效果上看,和load是保持一致的,最后還是會(huì)將解析出來的key-value添加到自身去。
這種設(shè)計(jì)可以同時(shí)load多個(gè)配置文件,先load的配置有可能被覆蓋
要修改或增加配置,最好調(diào)用java.util.Properties.setProperty(String, String)方法,即使它只是做了代理
public synchronized Object setProperty(String key, String value) {
return put(key, value);
}
2.輸出
Properties提供了list、save、store一系列方法幫助我們將配置寫入輸出流中,具體方法請(qǐng)alt+/
其中java.util.Properties.save(OutputStream, String)方法已廢棄,可以看到它代理了java.util.Properties.store(OutputStream, String)方法,我們使用時(shí)直接調(diào)用store系列方法即可
java.util.Properties.store(OutputStream, String)和java.util.Properties.store(Writer, String)方法是java.util.Properties.store0(BufferedWriter, String, boolean)方法的代理,從源碼可以看出該方法遍歷打印了所有的key-value,list方法也大同小異
private void store0(BufferedWriter bw, String comments, boolean escUnicode)
throws IOException
{
if (comments != null) {
writeComments(bw, comments);
}
bw.write("#" + new Date().toString());
bw.newLine();
synchronized (this) {
for (Enumeration<?> e = keys(); e.hasMoreElements();) {
String key = (String)e.nextElement();
String val = (String)get(key);
key = saveConvert(key, true, escUnicode);
/* No need to escape embedded and trailing spaces for value, hence
* pass false to flag.
*/
val = saveConvert(val, false, escUnicode);
bw.write(key + "=" + val);
bw.newLine();
}
}
bw.flush();
}
java.util.Properties.storeToXML(OutputStream, String)、java.util.Properties.storeToXML(OutputStream, String, String)這兩個(gè)方法和java.util.Properties.loadFromXML(InputStream)方法一樣,最后最為調(diào)用的是sun.util.spi.XmlPropertiesProvider.store(Properties arg0, OutputStream arg1, String arg2, String arg3)方法,效果和java.util.Properties.store(OutputStream, String)和java.util.Properties.store(Writer, String)方法保持一致。
兩個(gè)list方法和兩個(gè)store方法類似。
補(bǔ)充:需要注意,properties.load進(jìn)去后,輸入流不會(huì)被關(guān)閉,在讀取完配置文件后,應(yīng)該手動(dòng)關(guān)閉掉輸入流