Java中Properties類(lèi)的操作實(shí)例詳解
Java中Properties類(lèi)的操作實(shí)例詳解
知識(shí)學(xué)而不用,就等于沒(méi)用,到真正用到的時(shí)候還得重新再學(xué)。最近在看幾款開(kāi)源模擬器的源碼,里面涉及到了很多關(guān)于Properties類(lèi)的引用,由于Java已經(jīng)好久沒(méi)用了,而這些模擬器大多用Java來(lái)寫(xiě),外加一些腳本語(yǔ)言Python,Perl之類(lèi)的,不得已,又得重新拾起。本文通過(guò)看《Java編程思想》和一些網(wǎng)友的博客總結(jié)而來(lái),只為簡(jiǎn)單介紹Properties類(lèi)的相關(guān)操作。
一、Java Properties類(lèi)
Java中有個(gè)比較重要的類(lèi)Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語(yǔ)言都有自己所支持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類(lèi)ConfigParse,方便程序員或用戶通過(guò)該類(lèi)的方法來(lái)修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內(nèi)容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來(lái)注釋。
Properties類(lèi)繼承自Hashtable,如下:

它提供了幾個(gè)主要的方法:
1. getProperty ( String key),用指定的鍵在此屬性列表中搜索屬性。也就是通過(guò)參數(shù) key ,得到 key 所對(duì)應(yīng)的 value。
2. load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對(duì))。通過(guò)對(duì)指定的文件(比如說(shuō)上面的 test.properties 文件)進(jìn)行裝載來(lái)獲取該文件中的所有鍵 - 值對(duì)。以供 getProperty ( String key) 來(lái)搜索。
3. setProperty ( String key, String value) ,調(diào)用 Hashtable 的方法 put 。他通過(guò)調(diào)用基類(lèi)的put方法來(lái)設(shè)置 鍵 - 值對(duì)。
4. store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對(duì))寫(xiě)入輸出流。與 load 方法相反,該方法將鍵 - 值對(duì)寫(xiě)入到指定的文件中去。
5. clear (),清除所有裝載的 鍵 - 值對(duì)。該方法在基類(lèi)中提供。
二、Java讀取Properties文件
Java讀取Properties文件的方法有很多,詳見(jiàn): Java讀取Properties文件的六種方法
但是最常用的還是通過(guò)java.lang.Class類(lèi)的getResourceAsStream(String name)方法來(lái)實(shí)現(xiàn),如下可以這樣調(diào)用:
InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫(xiě)程序的,用此一種足夠。
或者下面這種也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
三、相關(guān)實(shí)例
下面列舉幾個(gè)實(shí)例,加深對(duì)Properties類(lèi)的理解和記憶。
我們知道,Java虛擬機(jī)(JVM)有自己的系統(tǒng)配置文件(system.properties),我們可以通過(guò)下面的方式來(lái)獲取。
1、獲取JVM的系統(tǒng)屬性
import java.util.Properties;
public class ReadJVM {
public static void main(String[] args) {
Properties pps = System.getProperties();
pps.list(System.out);
}
}
結(jié)果:

2、隨便新建一個(gè)配置文件(Test.properties)
name=JJ Weight=4444 Height=3333
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("Test.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}
}
}
3、一個(gè)比較綜合的實(shí)例
根據(jù)key讀取value
讀取properties的全部信息
寫(xiě)入新的properties信息
//關(guān)于Properties類(lèi)常用的操作
public class TestProperties {
//根據(jù)Key讀取Value
public static String GetValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
System.out.println(key + " = " + value);
return value;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
//讀取Properties的全部信息
public static void GetAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
Enumeration en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}
}
//寫(xiě)入Properties信息
public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
//從輸入流中讀取屬性列表(鍵和元素對(duì))
pps.load(in);
//調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//強(qiáng)制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以適合使用 load 方法加載到 Properties 表中的格式,
//將此 Properties 表中的屬性列表(鍵和元素對(duì))寫(xiě)入輸出流
pps.store(out, "Update " + pKey + " name");
}
public static void main(String [] args) throws IOException{
//String value = GetValueByKey("Test.properties", "name");
//System.out.println(value);
//GetAllProperties("Test.properties");
WriteProperties("Test.properties","long", "212");
}
}
結(jié)果:
Test.properties中文件的數(shù)據(jù)為:
#Update long name #Sun Feb 23 18:17:16 CST 2014 name=JJ Weight=4444 long=212 Height=3333
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java中在時(shí)間戳計(jì)算的過(guò)程中遇到的數(shù)據(jù)溢出問(wèn)題解決
這篇文章主要介紹了Java中在時(shí)間戳計(jì)算的過(guò)程中遇到的數(shù)據(jù)溢出問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Springboot集成任務(wù)調(diào)度實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Springboot集成任務(wù)調(diào)度實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Java異常簡(jiǎn)介和架構(gòu)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要分享了Java異常簡(jiǎn)介和架構(gòu),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Java 內(nèi)省introspector相關(guān)原理代碼解析
這篇文章主要介紹了Java 內(nèi)省introspector相關(guān)原理代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java中Collection與Collections的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Collection與Collections的區(qū)別,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的同學(xué)可以參考一下2023-06-06

