1.簡單介紹
Java中有個比較重要的類Properties(Java.util.Properties),主要用于讀取Java的組態檔,各種語言都有自己所支持的組態檔,組態檔中很多變數是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程式本身去修改相關的變數設定,像Python支持的組態檔是.ini檔案,同樣,它也有自己讀取組態檔的類ConfigParse,方便程式員或用戶通過該類的方法來修改.ini組態檔,在Java中,其組態檔常為.properties檔案,格式為文本檔案,檔案的內容的格式是“鍵=值”的格式,文本注釋資訊可以用"#"來注釋,
Properties類繼承自Hashtable,如下:

它提供了幾個主要的方法:
- load(InputStream inStream),從輸入流中讀取屬性串列(鍵和元素對),通過對指定的檔案(比如說上面的 test.properties 檔案)進行裝載來獲取該檔案中的所有鍵-值對,以供 getProperty(Stringkey)來搜索,
- getProperty(String key),用指定的鍵在此屬性串列中搜索屬性,也就是通過引數key,得到key所對應的value,
- setProperty(String key, String value) ,呼叫 Hashtable 的方法 put ,他通過呼叫基類的put方法來設定 鍵-值對,
- remove(String key),用來洗掉指定的鍵,
- store (OutputStream out,String comments),以適合使用load方法加載到Properties 表中的格式,將此 Properties 表中的屬性串列(鍵和元素對)寫入輸出流,與 load方法相反,該方法將鍵-值對寫入到指定的檔案中去,
- clear(),清除所有裝載的 鍵-值對,該方法在基類中提供,
2.讀取Properties檔案
Java讀取Properties檔案的方法有很多,詳細可參考:Java讀取Properties檔案的六種方法
常用的有兩種:
通過java.lang.Class類的getResourceAsStream(String name)方法來實作
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
獲取resource資源檔案
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName)
String path = ClassLoader.getSystemResource(fileName).toURI().getPath()
3. 相關實體
下面介紹一些常用操作,方便加深對Properties的理解與記憶,
創建chan.properties
name=寧川
password=123456
[email protected]
讀取properties
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, charsetName);
properties.load(reader);
String email = properties.getProperty("email");
關于中文的問題:
上面可以看到,我們的name是中文,這是我們列印出來的值可能是亂碼的,
name=????·?
這時,我們首先要保證使用的IDEproperties編碼時UTF-8,
IDEA:檔案->設定->編輯器->檔案編碼
eclipse:右鍵該檔案->properties
然后可以在讀取的時候指定編碼,或者讀取后再轉換編碼,
同樣,保存的時候也指定UTF-8.
InputStream stream = ClassLoader.getSystemResourceAsStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, charsetName);
properties.load(stream);
String name;
name = new String(properties.getProperty("name", "小明").toString().getBytes(),"UTF-8");
關于Properties順序讀寫的問題:
Java 的 Properties 加載屬性檔案后是無法保證輸出的順序與檔案中一致的,因為 Properties 是繼承自 Hashtable 的, key/value 都是直接存在 Hashtable 中的,而 Hashtable 是不保證進出順序的,
總有時候會有關心順序一致的需求,恰如有 org.apache.commons.collections.OrderdMap(其實用 LinkedHashMap 就是保證順序)一樣,我們也想要有個 OrderdProperties,
import java.util.*;
public class OrderedProperties extends Properties {
private static final long serialVersionUID = -4627607243846121965L;
private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
@Override
public synchronized Enumeration<Object> keys() {
return Collections.<Object>enumeration(keys);
}
@Override
public synchronized Object put(Object key, Object value) {
keys.add(key);
System.out.println("key=" + key);
System.out.println("value=https://www.cnblogs.com/sivanchan/p/" + value);
return super.put(key, value);
}
@Override
public synchronized Object remove(Object key) {
keys.remove(key);
return super.remove(key);
}
@Override
public Set
練習用的PropertiesUtils,包含一些常用操作,
import java.io.*;
import java.util.*;
public class PropertiesUtils {
private Properties properties;
private String fileName;
private String charsetName = "UTF-8";
private Boolean autoCommit = true;
public PropertiesUtils(String name) {
loadProperties(name);
}
public PropertiesUtils(String name, String charsetName) {
this.charsetName = charsetName;
loadProperties(name);
}
public Properties getProperties() {
return properties;
}
public void setCharsetName(String charsetName) {
this.charsetName = charsetName;
}
public String getCharsetName() {
return charsetName;
}
public void setAutoCommit(Boolean autoCommit) {
this.autoCommit = autoCommit;
}
public Boolean getAutoCommit() {
return autoCommit;
}
/**
* 加載properties
* @param name 資源檔案properties
* @return
*/
void loadProperties(String name) {
fileName = name;
InputStream stream = null;
properties = new OrderedProperties();
try {
stream = ClassLoader.getSystemResourceAsStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, charsetName);
properties.load(reader);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* properties轉map
* @return Map<String, String>
*/
public Map<String, String> getPropToMap() {
Map<String, String> map = null;
if (properties != null) {
map = new HashMap<String, String>();
Enumeration enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
String value = https://www.cnblogs.com/sivanchan/p/properties.getProperty(key);
map.put(key, value);
}
}
return map;
}
/**
* 獲取key對應的值
* @param key key
* @return String
*/
public String getProperty(String key) {
return getProperty(key, null);
}
/**
* 獲取key對應的值
* @param key key
* @param defaultValue defaultValue
* @return String
*/
public String getProperty(String key, String defaultValue) {
if (properties != null && !properties.isEmpty()) {
return properties.getProperty(key, defaultValue);
}
return null;
}
/**
* 更新配置
* @param key key
* @param value vale
* @return Boolean
*/
public Boolean setProperty(String key, String value) {
return setProperty(key, value, "");
}
/**
* 更新配置
* @param key key
* @param value value
* @param comments 說明
* @return Boolean
*/
public Boolean setProperty(String key, String value, String comments) {
properties.setProperty(key, value);
if (autoCommit) {
return saveProperties(comments);
}
return true;
}
/**
* 洗掉key
* @param key key
* @return Boolean
*/
public Boolean remove(String key) {
return remove(key, "");
}
/**
* 洗掉key
* @param key key
* @param comments 說明
* @return Boolean
*/
public Boolean remove(String key, String comments) {
properties.remove(key);
if (autoCommit) {
return saveProperties(comments);
}
return true;
}
/**
* 保存Properties
* @return Boolean
*/
public Boolean saveProperties() {
return saveProperties("");
}
/**
* 保存Properties
* @param comments 說明
* @return Boolean
*/
public Boolean saveProperties(String comments) {
try {
String path = ClassLoader.getSystemResource(fileName).toURI().getPath();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path), charsetName);
properties.store(outputStreamWriter, comments);
outputStreamWriter.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 列印properties
*/
public void toList() {
Enumeration
測驗:
public class PropertiesReader {
public static void main(String[] args) {
String fileName = "chan.properties";
PropertiesUtils propUtils = new PropertiesUtils(fileName);
//查詢key->value
String key = "name";
String value = https://www.cnblogs.com/sivanchan/p/propUtils.getProperty(key);
System.out.printf("%s=%s%n", key, value);
System.out.println("--------------------------------");
//獲取map
Map propMap = propUtils.getPropToMap();
propMap.forEach((k, v) -> System.out.println(String.format("%s=%s", k, v)));
System.out.println("--------------------------------");
//更新值
propUtils.setProperty(key, "習慣");
System.out.println(propUtils.getProperty(key));
System.out.println("--------------------------------");
//新增值
propUtils.setProperty("hobby.one", "足球");
propUtils.setProperty("hobby.two", "football");
//洗掉
propUtils.remove("hobby.two");
//列印properties
new PropertiesUtils(fileName).toList();
}
}
繼承Properties,并重寫部分方法,經測驗確實可以實作順序讀寫,
OrderedProperties主要是通過Set保存properties的key,并重寫有關peoperties key的方法,但是目前只是重寫部分方法,
測驗程序中發現,remove并保存的時候,出現空指標例外,經排查發現是key的問題,于是在網上找的方法的基礎上,重寫了remove方法,
由于還沒看原始碼,可能還會有其他的問題,在這里記錄一下,
但是目前常用操作都有了,也ok.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/67407.html
標籤:Java
