淺談Java中Properties類的詳細使用
Properties 類位于 java.util.Properties ,是Java 語言的配置文件所使用的類, Xxx.properties 為Java 語言常見的配置文件,如數據庫的配置 jdbc.properties, 系統參數配置 system.properties。 這里,講解一下Properties 類的具體使用。以key=value 的 鍵值對的形式進行存儲值。 key值不能重復。
繼承了Hashtable 類,以Map 的形式進行放置值, put(key,value) get(key)
主要方法:
這里只講解一些常用的形式。
二、打印JVM參數JVM 中可以獲取Properties, 來打印輸出 JVM 所了解的屬性值。用list() 方法,打印到控制臺。
@Testpublic void printTest(){ Properties properties=System.getProperties(); properties.list(System.out);}
常見的有:
在src 目錄下,放置 jdbc.properties 文件,是數據庫的配置文件。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=abc123
3.1、list輸出到控制臺用絕對路徑加載@Testpublic void name1Test(){ try{Properties properties=new Properties();//用的是磁盤符的絕對路徑 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值轉換成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(); }}
輸出時,getProperty() 有當前的key值,則輸出Key值對應的value 值。如果沒有key值,則輸出 null 值。后面可以跟 default 值,如果沒有該值,則輸出設置的默認值。
@Testpublic void writeTest(){ try{Properties properties=new Properties();InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream('jdbc.properties');properties.load(input);//多添加幾個值。properties.setProperty('name','兩個蝴蝶飛');properties.setProperty('sex','男');//properties.put('name','兩個蝴蝶飛'); 可以用繼承Hashtable 的put 方法寫入值// properties.put('sex','男');//將添加的值,連同以前的值一起寫入 新的屬性文件里面。OutputStream out=new FileOutputStream('D:jdbc.properties');properties.store(out,'填充數據'); }catch(Exception e){e.printStackTrace(); }}
在構建輸入流和輸出流時,指定編碼格式, 編碼的格式相同。 如均是 utf-8的形式。
@Testpublic void write2Test(){ try{Properties properties=new Properties();//用絕對路徑InputStream input=new BufferedInputStream(new FileInputStream('D:workspaceJavaLearnsrcjdbc.properties'));properties.load(new InputStreamReader(input,'utf-8')); //多添加幾個值。properties.setProperty('name','兩個蝴蝶飛');properties.setProperty('sex','男');OutputStream output=new FileOutputStream('D:jdbc.properties');OutputStreamWriter out=new OutputStreamWriter(output,'utf-8');properties.store(out,'填充數據'); }catch(Exception e){e.printStackTrace(); }}
測試運行之后:
這樣便解決了亂碼的問題。
六、加載和導出到xml配置文件6.1、導出到.xml配置文件storeToXML將Properties 類中定義的屬性,導出成 .xml 的形式.
@Testpublic void xmlWriteTest(){ try{//處理成編碼樣式。Properties properties=new Properties(); //多添加幾個值。properties.setProperty('name','兩個蝴蝶飛');properties.setProperty('sex','男');OutputStream output=new FileOutputStream('D:jdbc.xml');//編碼設置成utf-8的形式。 properties.storeToXML(output,'填充到xml','utf-8'); }catch(Exception e){e.printStackTrace(); }}
測試結果為:
用 <entry> 節點 key為屬性, 后面跟值來進行輸入??砂凑者@種形式,繼續添加。
6.2、導出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(); }}
以上就是淺談Java中Properties類的詳細使用的詳細內容,更多關于Java Properties的資料請關注好吧啦網其它相關文章!
相關文章: