Java中Properties 類的詳細(xì)使用
我把你的頭像,設(shè)置成我的名字,此刻你便與我同在。我把你的名字,寫進(jìn)我的代碼里面,以后,我的世界便存在著你。
一.Properties 類Properties 類位于 java.util.Properties ,是Java 語言的配置文件所使用的類, Xxx.properties 為Java 語言常見的配置文件,如數(shù)據(jù)庫(kù)的配置 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)。
@Testpublic void printTest(){Properties properties=System.getProperties();properties.list(System.out);}
常見的有:
在src 目錄下,放置 jdbc.properties 文件,是數(shù)據(jù)庫(kù)的配置文件。
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8jdbc.username=rootjdbc.password=abc123三.一 list 輸出到控制臺(tái) 用絕對(duì)路徑加載
@Testpublic void name1Test(){try{Properties properties=new Properties();//用的是磁盤符的絕對(duì)路徑 InputStream input=new BufferedInputStream(new FileInputStream('D:workspaceJavaLearnsrcjdbc.properties'));properties.load(input);properties.list(System.out);}catch(Exception e){e.printStackTrace();}}
url 被截取了。
@Testpublic 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();}}
@Testpublic void name3Test(){try{Properties properties=new Properties();//直接寫src 類路徑下的文件名InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream('jdbc.properties');properties.load(input);//把key值轉(zhuǎn)換成set 的形式,遍歷setSet<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();}}
@Testpublic 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)值。
@Testpublic 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();}}
在構(gòu)建輸入流和輸出流時(shí),指定編碼格式, 編碼的格式相同。 如均是 utf-8的形式。
@Testpublic void write2Test(){try{Properties properties=new Properties();//用絕對(duì)路徑InputStream input=new BufferedInputStream(new FileInputStream('D:workspaceJavaLearnsrcjdbc.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 配置文件六.一 導(dǎo)出到 .xml 配置文件 storeToXML將Properties 類中定義的屬性,導(dǎo)出成 .xml 的形式.
@Testpublic 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ù)添加。
六.二 導(dǎo)出XML 配置文件 loadFromXML@Testpublic 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();}}
這就是Properties 類的常見用法 。
到此這篇關(guān)于Java中Properties 類的詳細(xì)使用的文章就介紹到這了,更多相關(guān)Properties 類使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. arcgis js完整懸停效果實(shí)現(xiàn)demo2. .Net中Task Parallel Library的基本用法3. JavaScript實(shí)現(xiàn)form提交,回車提交URL地址偽靜態(tài) 原創(chuàng)4. Android 通過cmake的方式接入opencv的方法步驟5. ASP.NET Core自定義中間件的方式詳解6. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(二)7. css代碼優(yōu)化的12個(gè)技巧8. ASP.NET MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能9. 使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法10. jsp實(shí)現(xiàn)登錄界面
