淺談Java中Properties類(lèi)的詳細(xì)使用
一、Properties 類(lèi)
Properties 類(lèi)位于 java.util.Properties ,是Java 語(yǔ)言的配置文件所使用的類(lèi), Xxx.properties 為Java 語(yǔ)言常見(jiàn)的配置文件,如數(shù)據(jù)庫(kù)的配置 jdbc.properties, 系統(tǒng)參數(shù)配置 system.properties。 這里,講解一下Properties 類(lèi)的具體使用。
以key=value 的 鍵值對(duì)的形式進(jìn)行存儲(chǔ)值。 key值不能重復(fù)。
繼承了Hashtable 類(lèi),以Map 的形式進(jìn)行放置值, put(key,value) get(key)
主要方法:
這里只講解一些常用的形式。
二、打印JVM參數(shù)
JVM 中可以獲取Properties, 來(lái)打印輸出 JVM 所了解的屬性值。
用list() 方法,打印到控制臺(tái)。
@Test public void printTest(){ Properties properties=System.getProperties(); properties.list(System.out); }
常見(jiàn)的有:
三、打印自定義.properties文件中的值
在src 目錄下,放置 jdbc.properties 文件,是數(shù)據(jù)庫(kù)的配置文件。
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(); //用的是磁盤(pán)符的絕對(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(); //直接寫(xiě)src 類(lèi)路徑下的文件名 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","沒(méi)有該key值"); System.out.println("輸出值:"+value); }catch(Exception e){ e.printStackTrace(); } }
輸出時(shí),getProperty() 有當(dāng)前的key值,則輸出Key值對(duì)應(yīng)的value 值。
如果沒(méi)有key值,則輸出 null 值。
后面可以跟 default 值,如果沒(méi)有該值,則輸出設(shè)置的默認(rèn)值。
五、寫(xiě)入到Properties文件
5.1、普通寫(xiě)入,中文時(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 方法寫(xiě)入值 // properties.put("sex","男"); //將添加的值,連同以前的值一起寫(xiě)入 新的屬性文件里面。 OutputStream out=new FileOutputStream("D:\\jdbc.properties"); properties.store(out,"填充數(shù)據(jù)"); }catch(Exception e){ e.printStackTrace(); } }
5.2、解決亂碼寫(xiě)入的問(wèn)題
在構(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)行之后:
這樣便解決了亂碼的問(wèn)題。
六、加載和導(dǎo)出到xml配置文件
6.1、導(dǎo)出到.xml配置文件storeToXML
將Properties 類(lèi)中定義的屬性,導(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為屬性, 后面跟值來(lái)進(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類(lèi)的詳細(xì)使用的詳細(xì)內(nèi)容,更多關(guān)于Java Properties的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪(fǎng)問(wèn)跨域問(wèn)題
這篇文章主要介紹了Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪(fǎng)問(wèn)跨域問(wèn)題,關(guān)于Cors跨域的問(wèn)題,前端有代理和jsonp的常用方式解決這種非同源的訪(fǎng)問(wèn)拒絕策略2023-04-04java eclipse 整個(gè)項(xiàng)目或包查找只定字符串并替換操作
這篇文章主要介紹了java eclipse 整個(gè)項(xiàng)目或包查找只定字符串并替換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09Java獲得一個(gè)數(shù)組的指定長(zhǎng)度排列組合算法示例
這篇文章主要介紹了Java獲得一個(gè)數(shù)組的指定長(zhǎng)度排列組合算法,結(jié)合實(shí)例形式分析了java排列組合相關(guān)數(shù)組遍歷、運(yùn)算操作技巧,需要的朋友可以參考下2019-06-06SpringBoot+mail 輕松實(shí)現(xiàn)各類(lèi)郵件自動(dòng)推送
在實(shí)際的項(xiàng)目開(kāi)發(fā)過(guò)程中,經(jīng)常需要用到郵件通知功能,例如,通過(guò)郵箱注冊(cè),郵箱找回密碼,郵箱推送報(bào)表等等,實(shí)際的應(yīng)用場(chǎng)景非常的多,今天通過(guò)這篇文章,我們一起來(lái)學(xué)習(xí)如何在 Spring Boot 中快速實(shí)現(xiàn)一個(gè)自動(dòng)發(fā)送郵件的功能2024-07-07Spring 靜態(tài)變量/構(gòu)造函數(shù)注入失敗的解決方案
我們經(jīng)常會(huì)遇到一下問(wèn)題:Spring對(duì)靜態(tài)變量的注入為空、在構(gòu)造函數(shù)中使用Spring容器中的Bean對(duì)象,得到的結(jié)果為空。不要擔(dān)心,本文將為大家介紹如何解決這些問(wèn)題,跟隨小編來(lái)看看吧2021-11-11Java EE項(xiàng)目中的異常處理總結(jié)(一篇不得不看的文章)
什么是異常?運(yùn)行時(shí)發(fā)生的可被捕獲和處理的錯(cuò)誤。這篇文章主要介紹了Java EE項(xiàng)目中的異常處理總結(jié),有需要的可以了解一下。2016-11-11使用ServletUtil.write方法下載接口文件中文亂碼問(wèn)題解決
本文主要介紹了使用ServletUtil.write方法下載接口文件中文亂碼問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05java并發(fā)編程專(zhuān)題(五)----詳解(JUC)ReentrantLock
這篇文章主要介紹了java(JUC)ReentrantLock的的相關(guān)資料,文中講解非常詳細(xì),實(shí)例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07