首頁  >  文章  >  Java  >  Properties與ResourceBundle的詳細介紹

Properties與ResourceBundle的詳細介紹

零下一度
零下一度原創
2017-07-20 10:37:411449瀏覽

1.Properties與ResourceBundle

兩個類別都可以讀取屬性檔案中以key/value形式儲存的鍵值對,ResourceBundle讀取屬性檔時操作相對簡單

2.Properties

此類別繼承Hashtable,將鍵值對儲存在集合中。基於輸入流從屬性檔讀取鍵值對,load()方法調用完畢,就與輸入流脫離關係,不會自動關閉輸入流,需要手動關閉。

    /** * 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中,
     * 通过put方法可以向集合中添加键值对或者修改key对应的value
     * 
     * @throws IOException     */@SuppressWarnings("rawtypes")
    @Testpublic void test01() throws IOException {
        FileInputStream fis = new FileInputStream("Files/test01.properties");
        Properties props = new Properties();
        props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭/*byte[] buf = new byte[1024];
        int length = fis.read(buf);
        System.out.println("content=" + new String(buf, 0, length));//抛出StringIndexOutOfBoundsException*/System.out.println("driver=" + props.getProperty("jdbc.driver"));
        System.out.println("url=" + props.getProperty("jdbc.url"));
        System.out.println("username=" + props.getProperty("jdbc.username"));
        System.out.println("password=" + props.getProperty("jdbc.password"));/** * Properties其他可能用到的方法         */props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)props.put("jdbc.password", "456");
        FileOutputStream fos = new FileOutputStream("Files/test02.xml");// 将Hashtable中的数据写入xml文件中props.storeToXML(fos, "来自属性文件的数据库连接四要素");

        System.out.println();
        System.out.println("遍历属性文件");
        System.out.println("hashtable中键值对数目=" + props.size());
        Enumeration keys = props.propertyNames();while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            System.out.println(key + "=" + props.getProperty(key));
        }

    }

3.ResourceBundle

該類別基於類別讀取屬性檔案:將屬性檔案當作類,意味著屬性檔案必須放在套件中,使用屬性檔案的全限定性類別名稱而非路徑指涉屬性檔案。

    /** * 基于类读取属性文件:该方法将属性文件当作类来处理,属性文件放在包中,使用属性文件的全限定性而非路径来指代文件     */@Testpublic void test02() {
        ResourceBundle bundle = ResourceBundle.getBundle("com.javase.properties.test01");
        System.out.println("获取指定key的值");
        System.out.println("driver=" + bundle.getString("jdbc.driver"));
        System.out.println("url=" + bundle.getString("jdbc.url"));
        System.out.println("username=" + bundle.getString("jdbc.username"));
        System.out.println("password=" + bundle.getString("jdbc.password"));

        System.out.println("-----------------------------");
        System.out.println("遍历属性文件");
        Enumeration<String> keys = bundle.getKeys();while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            System.out.println(key + "=" + bundle.getString(key));
        }
    }

4.讀取屬性檔案到Spring容器

通常將資料庫連接四要素放在屬性文件中,程式從屬性檔讀取參數,這樣當資料庫連線要素改變時,就不需要修改原始碼。將屬性檔案中的內容載入到xml文件中的方法:

  1. 在設定檔頭配置context約束。

  2. 在設定檔中加入,載入屬性到設定檔中。

  3. 取得設定檔內容:${key}

5.註解

#放在前面,用於在屬性檔案中新增註解。

6.編碼

屬性檔採用ISO-8859-1編碼方式,該編碼方式不支援中文,中文字元將會轉化為Unicode編碼方式顯示。

以上是Properties與ResourceBundle的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn