1、簡介
在項目中我們經常看到一種格式極其干凈的配置文件,如:config.properties。在Java的體系結構中也提供了API對properties文件進行讀取和寫入等操作,即:Properties類。
2、入門DEMO
在cn.lay.properties包下建立類Properties.java和config.properties文件,如下:
Properties.java
package cn.lay.properties; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) throws IOException { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties"); Properties config = new Properties(); config.load(inputStream); String userName = config.getProperty("username"); System.out.println("username=" + userName); } }
config.properties
username=lay
運行main方法,輸出:
username=lay
main方法中,通過輸入流讀取了config.properties。Properties實例對象從流中讀取文件屬性,並提供getProperty(key)方法讀取屬性。
3、類Properties
類Properties存在於java.util包下
繼承結構如:
java.lang.Object
|_ java.uil.Dictionary<K,V>
|_ java.util.Hashtable<Object,Object>
|_ java.util.Properties
已實現的主要接口:
Serializable, Cloneable, Map<Object, Object>
直接子類:
Provider
Properties直接繼承自Hashtable那么它的數據結構也和Hashtable一樣屬於鍵值對形式如:username="lay",不過不同的是,Properties的鍵和值都是String類型。所以,雖然Properties繼承了Hashtable后可以使用put和putAll方法,但是不被建議使用。因為這兩個方法允許插入非String類型。
字段摘要:
protected Properties defaults; 默認屬性列表
構造函數:
1) Properties();
2) Properties(Properties defaults); 可以初始化默認屬性列表
4、加載properties資源文件
Properties類重載了兩個方法用於讀取屬性列表,也就是加載資源為實例對象:
1) void load(InputStream inputStream);
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties"); Properties config = new Properties(); config.load(inputStream);
2) void load(Reader reader);
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cn/lay/properties/config.properties"); Reader reader = new InputStreamReader(inputStream, "utf-8"); Properties config = new Properties(); config.load(reader);
5、讀取屬性值
1)String getProperty(String key);
String userName = config.getProperty("username");
2) String getProperty(String key, String defaultValue);
String userName = config.getProperty("username", "nobody");
6、設置屬性值
config.setProperty("username", "marry");
7、存儲為properties資源文件
寫數據的方法分為兩種,list和store
1)list 此方法通常用於調試,System.out即可以獲取PrintStream,從而輸出到控制台
void list(PrintStream out);
PrintStream printStream = new PrintStream("/Users/lay-mac/Desktop/config.properties"); config.list(printStream);
void list(PrintWriter writer);
PrintWriter printWriter = new PrintWriter(outputStream); config.list(printWriter);
2) store
void store(OutputStream out, String comments);
OutputStream outputStream = new FileOutputStream("/Users/lay-mac/Desktop/config.properties"); config.store(outputStream, "test store");
void store(Writer writer, String comments);
Writer writer = new FileWriter("/Users/lay-mac/Desktop/config.properties"); config.store(writer, "test store");
8、遍歷屬性列表
Set<String> stringPropertyNames(); 返回屬性列表鍵的set集合,包括默認列表;
Set<String> keySet = config.stringPropertyNames(); for (String key : keySet) { System.out.println("key=" + key); System.out.println("value=" + config.getProperty(key)); }
Enumeration<?> propertyNames();返回屬性列表中所有鍵的枚舉,包括默認列表;
Enumeration<String> enumeration = (Enumeration<String>) config.propertyNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); System.out.println("key=" + key); System.out.println("value=" + config.getProperty(key)); }
除了讀取寫入.properties文件外,Properties類還可以讀取和寫入xml文件形式,具體請參考:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh