Java知識點整理正在進行中,關注我,持續給您帶來簡單,實用的Java程式設計技巧。
Properties類
1. Properties類的介紹
在Java中提供了 java.util.Properties 類,來讀取 .properties 屬性檔案。在程式調用 Properties 類的 load() 方法時,系統把 .properties 屬性檔案的内容加載到記憶體中。因為 Properties 類繼承了 Hashtable 類,Properties 類把“=”之間的内容,添加到Hashtable 對象的 key 值,并同時添加 key 值對應的 value 值,也就是“=”右側的值。所有在編寫 .properties 屬性檔案時一定要用“=”号把名稱與值分隔開。
通過 .properties 屬性文形式隻能儲存 String 類型資訊。
Properties 類是線程安全的,多個線程可以共享一個Properties對象,而不需要外部同步。
Properties 類的構造方法
構造方法 | 說明 |
Properties() | 建立一個無預設值的空屬性清單。 |
Properties(Properties defaults) | 建立一個帶有指定預設值的空屬性清單。 |
Properties類的常用方法
方法 | 說明 |
void load(InputStream inStream) | 從輸入流中讀取屬性清單(鍵和元素對)。 |
String getProperty(String key) | 用指定的鍵在此屬性清單中搜尋屬性。 |
String getProperty(String key, String defaultValue) | 用指定的鍵在屬性清單中搜尋屬性。 |
void list(PrintStream out) | 将屬性清單輸出到指定的輸出流。 |
void list(PrintWriter out) | 将屬性清單輸出到指定的輸出流。 |
void loadFromXML(InputStream in) | 将指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。 |
Enumeration<?> propertyNames() | 傳回屬性清單中所有鍵的枚舉,如果在主屬性清單中未找到同名的鍵,則包括預設屬性清單中不同的鍵。 |
Object setProperty(String key, String value) | 調用 Hashtable 的方法 put。 |
void store(OutputStream out, String comments) | 以适合使用 load 方法加載到 Properties 表中的格式,将此 Properties 表中的屬性清單(鍵和元素對)寫入輸出流。 |
void storeToXML(OutputStream os, String comment) | 發出一個表示此表中包含的所有屬性的 XML 文檔。 |
void storeToXML(OutputStream os, String comment, String encoding) | 使用指定的編碼發出一個表示此表中包含的所有屬性的 XML 文檔。 |
2. 示例:Properties類的使用
(1)在項目的預設路徑(src目錄)下建立 my.properties 屬性檔案(名稱可以自定義,擴充名必須為 properties )。
name=Jack
gender=male
(2)編寫讀取 properties 屬性檔案,并輸出屬性值。
package com.bbzd.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 示範Properties類的使用
* 在Java中提供了 java.util.Properties 類,來讀取 .properties 屬性檔案。在程式調用 Properties 類的 load() 方法時,
* 系統把 .properties 屬性檔案的内容加載到記憶體中。因為 Properties 類繼承了 Hashtable 類,Properties 類把“=”之間的内容,
* 添加到Hashtable 對象的 key 值,并同時添加 key 值對應的 value 值,也就是“=”右側的值。所有在編寫 .properties 屬性檔案
* 時一定要用“=”号把名稱與值分隔開。
* 通過 .properties 屬性文形式隻能儲存 String 類型資訊。
* Properties 類是線程安全的,多個線程可以共享一個Properties對象,而不需要外部同步。
* @date 2022/9/29 - 14:14
*/
public class PropertiesDemo {
public static void main(String[] args) {
//示範讀取配置檔案的用法
demo1();
//示範列印JVM參數的用法
demo2();
}
/**
* 示範讀取配置檔案的用法
*/
private static void demo1(){
// 建立Properties類對象
Properties properties=new Properties();
// 讀取my.properties配置檔案到輸入流中
InputStream inputStream=PropertiesDemo.class.getResourceAsStream("/my.properties");
try {
// 從輸入流中加載屬性清單
properties.load(inputStream);
// 擷取properties檔案中的屬性值
String name=properties.getProperty("name");
String gender=properties.getProperty("gender");
// 輸出結果
System.out.println("name: "+name);
System.out.println("gender: "+gender);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 示範列印JVM參數的用法
*/
private static void demo2(){
Properties properties=System.getProperties();
properties.list(System.out);
}
}
(3)運作結果
name: Jack
gender: male
3. 總結
(1)讀取 .properties 屬性檔案時,使用 Class 對象的 getResourceAsStream()方法,把指定的屬性檔案讀入輸入流中,并使用 Properties 類中的 load() 方法,從輸入流中讀取屬性清單(鍵/值對)。
(2)在使用 Class 對象的 load() 方法加載 .properties 屬性檔案的輸入流後,資料在記憶體中是以 Hashtable 的形式進行儲存的。
(3)使用 Properties 類的 getProperty() 方法,通過 key 鍵讀取出 value 值。