Properties 類代表一組持久的屬性。屬性可以保存到流中或從流中加載。它屬於java.util包。屬性定義以下實例變量。該變量保存與 Properties 對象關聯的默認屬性列表。
Properties defaults: This variable holds a default property list associated with a Properties object.
Properties類的特點:
- 屬性是一個子類哈希表.
- 它用於維護一個值列表,其中鍵是字符串,值也是字符串,即;它可用於在屬性文件中存儲和檢索字符串類型數據。
- Properties 類可以指定其他屬性列表,因為它是默認的。如果原始屬性列表中不存在特定的關鍵屬性,則將搜索默認屬性。
- Properties 對象不需要外部同步,多個線程可以共享單個 Properties 對象。
- 此外,它還可用於檢索係統的屬性。
優點 屬性文件
如果屬性記錄中的任何數據發生更改,則無需重新編譯 java 類。它用於存儲需要經常更改的數據。
注意:Properties 類沒有從其超類繼承負載因子的概念,哈希表.
聲明
public class Properties extends Hashtable<Object,Object>
屬性的構造函數
1. Properties():這將創建一個沒有默認值的 Properties 對象。
Properties p = new Properties();
2. Properties(Properties propDefault):第二個創建一個使用 propDefault 作為默認值的對象。
Properties p = new Properties(Properties propDefault);
示例 1:下麵的程序展示了如何使用Properties類從屬性文件中獲取信息。
讓我們創建一個屬性文件並將其命名為 db.properties。
db.properties
username = coder password = geeksforgeeks
代碼
Java
// Java program to demonstrate Properties class to get
// information from the properties file
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] args) throws Exception
{
// create a reader object on the properties file
FileReader reader = new FileReader("db.properties");
// create properties object
Properties p = new Properties();
// Add a wrapper around reader object
p.load(reader);
// access properties data
System.out.println(p.getProperty("username"));
System.out.println(p.getProperty("password"));
}
}
輸出
示例 2:下麵的程序展示了如何使用Properties類來獲取所有係統屬性。使用System.getProperties()方法,我們可以獲取係統的所有屬性。
Java
// Java program to demonstrate Properties class to get all
// the system properties
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] args) throws Exception
{
// get all the system properties
Properties p = System.getProperties();
// stores set of properties information
Set set = p.entrySet();
// iterate over the set
Iterator itr = set.iterator();
while (itr.hasNext()) {
// print each property
Map.Entry entry = (Map.Entry)itr.next();
System.out.println(entry.getKey() + " = "
+ entry.getValue());
}
}
}
輸出
示例 3:下麵的程序展示了如何使用Properties類來創建屬性文件。
Java
// Java program to demonstrate Properties class to create
// the properties file
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] args) throws Exception
{
// create an instance of Properties
Properties p = new Properties();
// add properties to it
p.setProperty("name", "Ganesh Chowdhary Sadanala");
p.setProperty("email",
"ganeshs.gfg@gmail.com");
// store the properties to a file
p.store(new FileWriter("info.properties"),
"GeeksforGeeks Properties Example");
}
}
輸出
屬性方法
METHOD |
DESCRIPTION |
---|---|
getProperty(String key) | 在此屬性列表中搜索具有指定鍵的屬性。 |
getProperty(String key, String defaultValue) | 在此屬性列表中搜索具有指定鍵的屬性。 |
list(PrintStream out) | 將此屬性列表打印到指定的輸出流。 |
Properties list(PrintWriter) | 將此屬性列表打印到指定的輸出流。 |
load(InputStream inStream) | 從輸入字節流中讀取屬性列表(鍵和元素對)。 |
load(Reader reader) | 以簡單的麵向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。 |
loadFromXML(InputStream in) | 將指定輸入流上的 XML 文檔表示的所有屬性加載到此屬性表中。 |
propertyNames() | 返回此屬性列表中所有鍵的枚舉,如果尚未從主屬性列表中找到同名鍵,則包括默認屬性列表中的不同鍵。 |
save(OutputStream out, String comments) |
已棄用. 如果保存屬性列表時發生 I/O 錯誤,此方法不會引發 IOException。 |
setProperty(String key, String value) | 調用 Hashtable 方法 put。 |
store(OutputStream out, String comments) | 將此 Properties 表中的屬性列表(鍵和元素對)以適合使用 load(InputStream) 方法加載到 Properties 表中的格式寫入輸出流。 |
store(Writer writer, String comments) | 將此屬性表中的屬性列表(鍵和元素對)以適合使用 load(Reader) 方法的格式寫入輸出字符流。 |
storeToXML(OutputStream os, String comment) | 發出一個 XML 文檔,表示該表中包含的所有屬性。 |
storeToXML(OutputStream os, String comment, String encoding) | 使用指定的編碼發出表示此表中包含的所有屬性的 XML 文檔。 |
storeToXML(OutputStream os, String comment, Charset charset) | 使用指定的編碼發出表示此表中包含的所有屬性的 XML 文檔。 |
stringPropertyNames() | 從此屬性列表中返回一組不可修改的鍵,其中鍵及其對應的值是字符串,如果尚未從主屬性列表中找到同名的鍵,則包括默認屬性列表中的不同鍵。 |
類 java.util.Hashtable 中聲明的方法
METHOD |
DESCRIPTION |
---|---|
clear() | 清除此哈希表,使其不包含鍵。 |
clone() | 創建此哈希表的淺拷貝。 |
計算(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) | 嘗試計算指定鍵及其當前映射值的映射(如果沒有當前映射,則為 null)。 |
computeIfAbsent(K key, Function<? super K,? 擴展 V> 映射函數) | 如果指定的鍵尚未與值關聯(或映射為 null),則嘗試使用給定的映射函數計算其值並將其輸入到此映射中,除非 null。 |
computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) | 如果指定鍵的值存在且非空,則嘗試在給定鍵及其當前映射值的情況下計算新映射。 |
contains(Object value) | 測試某些鍵是否映射到此哈希表中的指定值。 |
containsKey(Object key) | 測試指定的對象是否是此哈希表中的鍵。 |
containsValue(Object value) | 如果此哈希表將一個或多個鍵映射到該值,則返回 true。 |
elements() | 返回此哈希表中值的枚舉。 |
entrySet() | 返回此映射中包含的映射的 Set 視圖。 |
equals(Object o) | 根據 Map 接口中的定義,比較指定的 Object 與此 Map 是否相等。 |
get(Object key) | 返回指定鍵映射到的值,如果此映射不包含該鍵的映射,則返回 null。 |
hashCode() | 根據 Map 接口中的定義返回此 Map 的哈希碼值。 |
isEmpty() | 測試此哈希表是否沒有鍵映射到值。 |
keys() | 返回此哈希表中鍵的枚舉。 |
keySet() | 返回此映射中包含的鍵的集合視圖。 |
merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) | 如果指定的鍵尚未與值關聯或與 null 關聯,則將其與給定的非 null 值關聯。 |
put(K key, V value) | 將此哈希表中的指定鍵映射到指定值。 |
putAll(Map<? 擴展 K,? 擴展 V> t) | 將指定映射中的所有映射複製到此哈希表。 |
rehash() | 增加該哈希表的容量並在內部重新組織該哈希表,以便更有效地容納和訪問其條目。 |
remove(Object key) | 從此哈希表中刪除鍵(及其對應的值)。 |
size() | 返回此哈希表中鍵的數量。 |
toString() | 以一組條目的形式返回此 Hashtable 對象的字符串表示形式,用大括號括起來並用 ASCII 字符“,”(逗號和空格)分隔。 |
values() | 返回此映射中包含的值的集合視圖。 |
接口 java.util.Map 中聲明的方法
METHOD |
DESCRIPTION |
---|---|
forEach(BiConsumer<?超級 K,?超級 V> 操作) | 對此映射中的每個條目執行給定的操作,直到處理完所有條目或該操作引發異常。 |
getOrDefault(Object key, V defaultValue) | 返回指定鍵映射到的值,如果此映射不包含該鍵的映射,則返回 defaultValue。 |
putIfAbsent(K key, V value) | 如果指定的鍵尚未與值關聯(或映射為 null),則將其與給定值關聯並返回 null,否則返回當前值。 |
remove(Object key, Object value) | 僅當指定鍵當前映射到指定值時,才刪除該條目。 |
replace(K key, V value) | 僅當指定鍵當前映射到某個值時才替換該條目。 |
replace(K key, V oldValue, V newValue) | 僅當當前映射到指定值時才替換指定鍵的條目。 |
ReplaceAll(BiFunction<? super K,? super V,? 擴展 V> 函數) | 將每個條目的值替換為在該條目上調用給定函數的結果,直到處理完所有條目或函數引發異常。 |
相關用法
- Java Properties loadFromXML()用法及代碼示例
- Java Properties propertyNames()用法及代碼示例
- Java Properties setProperty()用法及代碼示例
- Java Properties stringPropertyNames()用法及代碼示例
- Java Properties list(PrintStream)用法及代碼示例
- Java Properties keys()用法及代碼示例
- Java Properties isEmpty()用法及代碼示例
- Java Properties clear()用法及代碼示例
- Java Properties clone()用法及代碼示例
- Java Properties containsKey(value)用法及代碼示例
- Java Properties contains(value)用法及代碼示例
- Java Properties containsValue(value)用法及代碼示例
- Java Properties elements()用法及代碼示例
- Java Properties entrySet()用法及代碼示例
- Java Properties equals(value)用法及代碼示例
- Java Properties forEach(BiConsumer)用法及代碼示例
- Java Properties hashCode()用法及代碼示例
- Java Properties get(key)用法及代碼示例
- Java Properties getProperty(key)用法及代碼示例
- Java Properties keySet()用法及代碼示例
- Java Properties list(PrintWriter)用法及代碼示例
- Java Properties getProperty(key, defaultValue)用法及代碼示例
- Java Properties getOrDefault(key, defaultValue)用法及代碼示例
- Java Properties computeIfAbsent(Key, Function)用法及代碼示例
- Java Properties computeIfPresent(Key, BiFunction)用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Properties Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。