1. 程式人生 > >Properties類詳細解析

Properties類詳細解析

java.util
類 Properties
java.lang.Object
  java.util.Dictionary<K,V>
      java.util.Hashtable<Object,Object>
          java.util.Properties

所有已實現的介面:

Serializable, Cloneable, Map<Object,Object>

直接已知子類:

Provider
Public class Properties extends HashTable<Object,Object
>

Properties類表示了一個持久的屬性集,它是在一個檔案中儲存鍵值對兒的,其中鍵值對兒以等號分隔。Properties可儲存在流中或從流中載入。屬性列表中的每個鍵及其所對應的值都是字串。Properties類是執行緒安全的:多個執行緒可以共享單個Properties物件而無需進行外部同步。一組屬性示例:
foo=bar
fu=baz

一個屬性列表可包含另一個屬性列表作為它的“預設值”;如果未能在原有的屬性列表中搜索到屬性鍵,則搜尋第二個屬性列表。

如果在“不安全”的 Properties 物件(即包含非 String 的鍵或值)上呼叫 store 或 save 方法,則該呼叫將失敗。類似地,如果在“不安全”的 Properties 物件(即包含非 String 的鍵)上呼叫 propertyNames 或 list 方法,則該呼叫將失敗。

除了輸入/輸出流使用 ISO 8859-1 字元編碼外,load(InputStream) / store(OutputStream, String)方法與 load(Reader)/store(Writer, String)對的工作方式完全相同。

loadFromXML(InputStream)storeToXML(OutputStream, String, String)方法按簡單的 XML 格式載入和儲存屬性。預設使用 UTF-8 字元編碼,但如果需要,可以指定某種特定的編碼。XML 屬性文件具有以下 DOCTYPE 宣告:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

一下列出了Properties的方法摘要,有關每個方法的詳細使用情況,請參看API:

這裡寫圖片描述

示例練習一:如何裝載屬性檔案並列出它當前的一組鍵和值。

思路:傳遞屬性檔案的輸入流InputStream給load()方法,會將改屬性檔案中的每個鍵值對兒新增到Properties例項中;然後條用list()列出所有屬性或者使用getProperty()獲取單獨的屬性。(注意 list() 方法的輸出中鍵-值對的順序與它們在輸入檔案中的順序不一樣。 Properties 類在一個散列表(hashtable,事實上是一個 Hashtable 子類)中儲存一組鍵-值對,所以不能保證順序。 )

程式碼實現:

package com.lmb.thread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("test.properties"));//載入屬性檔案
            properties.list(System.out);//將屬性檔案中的鍵值對兒列印到控制檯
            properties.getProperty("foo");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

示例練習二:如何裝載XML版本的屬性檔案並列出它當前的一組鍵和值。(只有裝載方法有差異,其餘完全相同load(),loadFromXML())

package com.lmb.thread;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.loadFromXML(new FileInputStream("test.xml"));//載入屬性檔案
            properties.list(System.out);//將屬性檔案中的鍵值對兒列印到控制檯
            properties.getProperty("foo");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

示例練習三:如何將檔案儲存到屬性檔案中?

import java.util.*;
import java.io.*;

public class StoreXML {
  public static void main(String args[]) throws Exception {
    Properties prop = new Properties();
    prop.setProperty("one-two", "buckle my shoe");
    prop.setProperty("three-four", "shut the door");
    prop.setProperty("five-six", "pick up sticks");
    prop.setProperty("seven-eight", "lay them straight");
    prop.setProperty("nine-ten", "a big, fat hen");
    prop.storeToXML(new FileOutputStream("test.xml"), "saveXML");//將鍵值對兒儲存到XML檔案中
prop.store(new FileOutputStream("test.properties"), "saveProperties");//將鍵值對兒儲存到普通的屬性檔案中
    fos.close();
  }
}

將鍵值對兒儲存到XML檔案中的輸出結果如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Rhyme</comment>
<entry key="seven-eight">lay them straight</entry>
<entry key="five-six">pick up sticks</entry>
<entry key="nine-ten">a big, fat hen</entry>
<entry key="three-four">shut the door</entry>
<entry key="one-two">buckle my shoe</entry>
</properties>

將鍵值對兒儲存到普通的屬性檔案中輸出結果如下:

one-two=buckle my shoe
three-four=shut the door
five-six=pick up sticks
seven-eight=lay them straight
nine-ten=a big, fat hen

注意:從一個XML檔案中裝載一組屬性,其DTD檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>

在外圍 <properties> 標籤中包裝的是一個 <comment> 標籤,後面是任意數量的 <entry>標籤。對每一個 <entry> 標籤,有一個鍵屬性,輸入的內容就是它的值。

Properties獲取資料亂碼解決

1.原因

Properties呼叫load(InputStream)時,讀取檔案時使用的預設編碼為ISO-8859-1;當我們將中文放入到properties檔案中,通過getProperty(key)獲取值時,取到得資料是ISO-8859-1格式的,但是ISO-8859-1是不能識別中文的。

2.解決方法

通過getProperty()獲取的資料data既然是ISO-8859-1編碼的,就通過data.getByte(“iso-8859-1”)獲取獲取,使用new String(data.getByte(“iso-8859-1”),”UTF-8”)進行轉換。當然properties檔案的編碼型別需要和new String(Byte[],charset)中的第二個引數的編碼型別相同。