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

它提供了幾個主要的方法:
1. getProperty ( String key),用指定的鍵在此屬性列表中搜索屬性。也就是通過參數(shù) key ,得到 key 所對應(yīng)的 value。
2. load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。
3. setProperty ( String key, String value) ,調(diào)用 Hashtable 的方法 put 。他通過調(diào)用基類的put方法來設(shè)置 鍵 - 值對。
4. store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。
5. clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。
二、Java讀取Properties文件
Java讀取Properties文件的方法有很多但是最常用的還是通過java.lang.Class類的getResourceAsStream(String name)方法來實現(xiàn),如下可以這樣調(diào)用:
InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫程序的,用此一種足夠。
或者下面這種也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
三、相關(guān)實例
下面列舉幾個實例,加深對Properties類的理解和記憶。
我們知道,Java虛擬機(JVM)有自己的系統(tǒng)配置文件(system.properties),我們可以通過下面的方式來獲取。
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、隨便新建一個配置文件(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、一個比較綜合的實例
根據(jù)key讀取value
讀取properties的全部信息
寫入新的properties信息
//關(guān)于Properties類常用的操作
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);
}
}
//寫入Properties信息
public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
//從輸入流中讀取屬性列表(鍵和元素對)
pps.load(in);
//調(diào)用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調(diào)用 put 的結(jié)果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以適合使用 load 方法加載到 Properties 表中的格式,
//將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
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 2016 name=JJ Weight=4444 long=212 Height=3333
相關(guān)文章
Java實現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法
這篇文章主要介紹了Java實現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法,涉及Java針對數(shù)據(jù)庫的讀取及文件寫入等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法
這篇文章主要介紹了Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
SpringCloud Zuul和Gateway的實例代碼(搭建方式)
本文主要介紹了SpringCloudZuul和SpringCloudGateway的簡單示例,SpringCloudGateway是推薦使用的API網(wǎng)關(guān)解決方案,基于SpringFramework5和ProjectReactor構(gòu)建,具有更高的性能和吞吐量2025-02-02
關(guān)于Mybatis使用collection分頁問題
項目中mybatis分頁的場景是非常高頻的,當(dāng)使用ResultMap并配置collection做分頁的時候,我們可能會遇到獲取當(dāng)前頁的數(shù)據(jù)少于每頁大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁問題,感興趣的朋友一起看看吧2021-11-11
SpringBoot中多個實現(xiàn)的接口正確注入的六種方式
在SpringBoot中,正確注入多個接口實現(xiàn)包括使用@Autowired和@Qualifier、@Resource注解、構(gòu)造方法注入、@Primary注解、Java配置類以及將所有實現(xiàn)注入到List或Map中,感興趣的可以了解一下2024-10-10
SpringBoot中MybatisX插件的簡單使用教程(圖文)
MybatisX 是一款基于 IDEA 的快速開發(fā)插件,方便在使用mybatis以及mybatis-plus開始時簡化繁瑣的重復(fù)操作,本文主要介紹了SpringBoot中MybatisX插件的簡單使用教程,感興趣的可以了解一下2023-06-06

