Java中Properties 類的詳細使用
我把你的頭像,設定成我的名字,此刻你便與我同在。
我把你的名字,寫進我的程式碼裡面,以後,我的世界便存在著你。
一.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() 方法,列印到控制檯。
@Test public void printTest(){ Properties properties=System.getProperties(); properties.list(System.out); }
常見的有:
三.列印自定義.properties 檔案中的值
在src 目錄下,放置 jdbc.properties 檔案,是資料庫的配置檔案。
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 jdbc.username=root jdbc.password=abc123
三.一 list 輸出到控制檯 用絕對路徑載入
@Test public void name1Test(){ try{ Properties properties=new Properties(); //用的是磁碟符的絕對路徑 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 被截取了。
三.二 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(); } }
三.三 stringPropertyNames 輸出 getClassLoader 載入 (推薦)
@Test public void name3Test(){ try{ Properties properties=new Properties(); //直接寫src 類路徑下的檔名 InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(input); //把key值轉換成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(); } }
輸出時,getProperty() 有當前的key值,則輸出Key值對應的value 值。
如果沒有key值,則輸出 null 值。
後面可以跟 default 值,如果沒有該值,則輸出設定的預設值。
五. 寫入到Properties 檔案
五.一 普通寫入,中文時亂碼
@Test public 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的形式。
@Test public void write2Test(){ try{ Properties properties=new Properties(); //用絕對路徑 InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties")); properties.load(new InputStreamReader(input,"utf-8")); //多新增幾個值。 properties.setProperty("name","男"); OutputStream output=new FileOutputStream("D:\\jdbc.properties"); OutputStreamWriter out=new OutputStreamWriter(output,"utf-8"); properties.store(out,"填充資料"); }catch(Exception e){ e.printStackTrace(); } }
測試執行之後:
這樣便解決了亂碼的問題。
六 . 載入和匯出到 xml 配置檔案
六.一 匯出到 .xml 配置檔案 storeToXML
將Properties 類中定義的屬性,匯出成 .xml 的形式.
@Test public void xmlWriteTest(){ try{ //處理成編碼樣式。 Properties properties=new Properties(); //多新增幾個值。 properties.setProperty("name","男"); OutputStream output=new FileOutputStream("D:\\jdbc.xml"); //編碼設定成utf-8的形式。 properties.storeToXML(output,"填充到xml","utf-8"); }catch(Exception e){ e.printStackTrace(); } }
測試結果為:
用 <entry> 節點 key為屬性, 後面跟值來進行輸入。
可按照這種形式,繼續新增。
六.二 匯出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(); } }
這就是Properties 類的常見用法 。
到此這篇關於Java中Properties 類的詳細使用的文章就介紹到這了,更多相關Properties 類使用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!