淺談Java中Properties類的詳細(xì)使用
一、Properties 類
Properties 類位于 java.util.Properties ,是Java 語言的配置文件所使用的類, Xxx.properties 為Java 語言常見的配置文件,如數(shù)據(jù)庫的配置 jdbc.properties, 系統(tǒng)參數(shù)配置 system.properties。 這里,講解一下Properties 類的具體使用。
以key=value 的 鍵值對(duì)的形式進(jìn)行存儲(chǔ)值。 key值不能重復(fù)。

繼承了Hashtable 類,以Map 的形式進(jìn)行放置值, put(key,value) get(key)
主要方法:

這里只講解一些常用的形式。
二、打印JVM參數(shù)
JVM 中可以獲取Properties, 來打印輸出 JVM 所了解的屬性值。
用list() 方法,打印到控制臺(tái)。
@Test
public void printTest(){
Properties properties=System.getProperties();
properties.list(System.out);
}
常見的有:

三、打印自定義.properties文件中的值
在src 目錄下,放置 jdbc.properties 文件,是數(shù)據(jù)庫的配置文件。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=abc123
3.1、list輸出到控制臺(tái)用絕對(duì)路徑加載
@Test
public void name1Test(){
try{
Properties properties=new Properties();
//用的是磁盤符的絕對(duì)路徑
InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));
properties.load(input);
properties.list(System.out);
}catch(Exception e){
e.printStackTrace();
}
}
url 被截取了。

3.2、propertyNames輸出getClass()加載
@Test
public void name2Test(){
try{
Properties properties=new Properties(); // 用/文件名, / 表示根目錄
InputStream input=PropertiesTest.class.getClass().getResourceAsStream("/jdbc.properties");
properties.load(input);
Enumeration<String> names=(Enumeration<String>) properties.propertyNames();
while(names.hasMoreElements()){
//這是key值
String key=names.nextElement();
String value=properties.getProperty(key);
System.out.println(key+"="+value);
}
}catch(Exception e){
e.printStackTrace();
}
}

3.3、stringPropertyNames輸出getClassLoader加載(推薦)
@Test
public void name3Test(){
try{
Properties properties=new Properties();
//直接寫src 類路徑下的文件名
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//把key值轉(zhuǎn)換成set 的形式,遍歷set
Set<String> names=properties.stringPropertyNames();
Iterator<String> iterator=names.iterator();
while(iterator.hasNext()){
String key=iterator.next();
String value=properties.getProperty(key);
System.out.println(key+"="+value);
}
}catch(Exception e){
e.printStackTrace();
}
}

四、獲取值getProperties
@Test
public void name3Test(){
try{
Properties properties=new Properties();
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//String value=properties.getProperty("jdbc.url");
String value=properties.getProperty("jdbc.url1","沒有該key值");
System.out.println("輸出值:"+value);
}catch(Exception e){
e.printStackTrace();
}
}
輸出時(shí),getProperty() 有當(dāng)前的key值,則輸出Key值對(duì)應(yīng)的value 值。
如果沒有key值,則輸出 null 值。
后面可以跟 default 值,如果沒有該值,則輸出設(shè)置的默認(rèn)值。

五、寫入到Properties文件
5.1、普通寫入,中文時(shí)亂碼
@Test
public void writeTest(){
try{
Properties properties=new Properties();
InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(input);
//多添加幾個(gè)值。
properties.setProperty("name","兩個(gè)蝴蝶飛");
properties.setProperty("sex","男");
//properties.put("name","兩個(gè)蝴蝶飛"); 可以用繼承Hashtable 的put 方法寫入值
// properties.put("sex","男");
//將添加的值,連同以前的值一起寫入 新的屬性文件里面。
OutputStream out=new FileOutputStream("D:\\jdbc.properties");
properties.store(out,"填充數(shù)據(jù)");
}catch(Exception e){
e.printStackTrace();
}
}

5.2、解決亂碼寫入的問題
在構(gòu)建輸入流和輸出流時(shí),指定編碼格式, 編碼的格式相同。 如均是 utf-8的形式。
@Test
public void write2Test(){
try{
Properties properties=new Properties();
//用絕對(duì)路徑
InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));
properties.load(new InputStreamReader(input,"utf-8"));
//多添加幾個(gè)值。
properties.setProperty("name","兩個(gè)蝴蝶飛");
properties.setProperty("sex","男");
OutputStream output=new FileOutputStream("D:\\jdbc.properties");
OutputStreamWriter out=new OutputStreamWriter(output,"utf-8");
properties.store(out,"填充數(shù)據(jù)");
}catch(Exception e){
e.printStackTrace();
}
}
測(cè)試運(yùn)行之后:

這樣便解決了亂碼的問題。
六、加載和導(dǎo)出到xml配置文件
6.1、導(dǎo)出到.xml配置文件storeToXML
將Properties 類中定義的屬性,導(dǎo)出成 .xml 的形式.
@Test
public void xmlWriteTest(){
try{
//處理成編碼樣式。
Properties properties=new Properties();
//多添加幾個(gè)值。
properties.setProperty("name","兩個(gè)蝴蝶飛");
properties.setProperty("sex","男");
OutputStream output=new FileOutputStream("D:\\jdbc.xml");
//編碼設(shè)置成utf-8的形式。
properties.storeToXML(output,"填充到xml","utf-8");
}catch(Exception e){
e.printStackTrace();
}
}
測(cè)試結(jié)果為:

用 <entry> 節(jié)點(diǎn) key為屬性, 后面跟值來進(jìn)行輸入。
可按照這種形式,繼續(xù)添加。
6.2、導(dǎo)出XML配置文件loadFromXML
@Test
public void xmlReadTest(){
try{
Properties properties=new Properties();
InputStream input=new BufferedInputStream(new FileInputStream("D:\\jdbc.xml"));
properties.loadFromXML(input);
properties.list(System.out);
}catch(Exception e){
e.printStackTrace();
}
}

以上就是淺談Java中Properties類的詳細(xì)使用的詳細(xì)內(nèi)容,更多關(guān)于Java Properties的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot統(tǒng)計(jì)、監(jiān)控SQL運(yùn)行情況的方法詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)計(jì)、監(jiān)控SQL運(yùn)行情況的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
詳解Java的Struts框架中上傳文件和客戶端驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Java的Struts框架中上傳文件和客戶端驗(yàn)證的實(shí)現(xiàn),Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
java實(shí)現(xiàn)學(xué)生選課系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生選課系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解
這篇文章主要介紹了Java System.currentTimeMillis()時(shí)間的單位轉(zhuǎn)換與計(jì)算方式案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
關(guān)于Java中finalize析構(gòu)方法的作用詳解
構(gòu)造方法用于創(chuàng)建和初始化類對(duì)象,也就是說,構(gòu)造方法負(fù)責(zé)”生出“一個(gè)類對(duì)象,并可以在對(duì)象出生時(shí)進(jìn)行必要的操作,在這篇文章中會(huì)給大家簡(jiǎn)單介紹一下析構(gòu)方法,需要的朋友可以參考下2023-05-05
SpringBoot實(shí)現(xiàn)過濾器攔截器的耗時(shí)對(duì)比
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)過濾器攔截器的輸出接口耗時(shí)對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-06-06
spring boot使用sonarqube來檢查技術(shù)債務(wù)
今天小編就為大家分享一篇關(guān)于spring boot使用sonarqube來檢查技術(shù)債務(wù),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

