深入理解Java中的Properties類
前言
使用Properties出現(xiàn)中文亂碼可看我這篇文章:properties出現(xiàn)中文亂碼解決方法(萬(wàn)能)
1. 基本知識(shí)
Properties 類是 Java 中用于處理配置文件的工具類,它繼承自 Hashtable 類,實(shí)現(xiàn)了 Map 接口。
- 主要用于讀取和寫(xiě)入屬性文件,以鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù)。
- 配置文件通常以 .properties 為擴(kuò)展名,其中每一行表示一個(gè)屬性或配置項(xiàng)。
使用該類可以更好的處理文件:
- 配置文件管理: 主要用于讀取和保存應(yīng)用程序的配置信息,例如數(shù)據(jù)庫(kù)連接信息、用戶設(shè)置等。
- 國(guó)際化: 用于加載不同語(yǔ)言的資源文件,方便國(guó)際化應(yīng)用程序。
- 持久化: 可以將配置信息持久化到文件,以便下次程序啟動(dòng)時(shí)重新加載。
對(duì)于其方法可查看其API接口:Properties API接口
構(gòu)造方法如下:
| 方法 | 表述 |
|---|---|
| Properties() | 創(chuàng)建一個(gè)沒(méi)有默認(rèn)值的空屬性列表。 |
| Properties?(int initialCapacity) | 創(chuàng)建一個(gè)沒(méi)有默認(rèn)值的空屬性列表,并且初始大小容納指定數(shù)量的元素,而無(wú)需動(dòng)態(tài)調(diào)整大小。 |
| Properties?(Properties defaults) | 創(chuàng)建具有指定默認(rèn)值的空屬性列表。 |
常用的方法如下:
| 返回類型 | 方法 | 表述 |
|---|---|---|
| String | getProperty?(String key) getProperty?(String key, String defaultValue) | 在此屬性列表中搜索具有指定鍵的屬性。 |
| void | list?(PrintStream out) list?(PrintWriter out) | 將此屬性列表打印到指定的輸出流。 |
| void | load?(InputStream inStream) | 從輸入字節(jié)流中讀取屬性列表(鍵和元素對(duì))。 |
| void | load?(Reader reader) | 以簡(jiǎn)單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對(duì))。 |
| Object | setProperty?(String key, String value) | 調(diào)用 Hashtable方法 put 。 |
| void | store?(OutputStream out, String comments) | 將此 Properties表中的此屬性列表(鍵和元素對(duì))以適合使用 load(InputStream)方法加載到 Properties表的格式寫(xiě)入輸出流。 |
| void | store?(Writer writer, String comments) | 將此 Properties表中的此屬性列表(鍵和元素對(duì))以適合使用 load(Reader)方法的格式寫(xiě)入輸出字符流。 |
2. 代碼示例
當(dāng)使用 Properties 類時(shí),你可以使用上述方法來(lái)讀取和寫(xiě)入屬性。以下是這些方法的一些簡(jiǎn)單的代碼示例:
1. getProperty(String key)
Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
properties.load(input);
// 獲取屬性值
String value = properties.getProperty("key");
System.out.println("Value for key 'key': " + value);
} catch (IOException e) {
e.printStackTrace();
}
2. getProperty(String key, String defaultValue)
Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
properties.load(input);
// 獲取屬性值,如果不存在則使用默認(rèn)值
String value = properties.getProperty("nonexistentKey", "default");
System.out.println("Value for key 'nonexistentKey': " + value);
} catch (IOException e) {
e.printStackTrace();
}
3. list(PrintStream out) / list(PrintWriter out)
Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
properties.load(input);
// 打印屬性列表到控制臺(tái)
properties.list(System.out);
} catch (IOException e) {
e.printStackTrace();
}
4. load(InputStream inStream)
Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
// 從輸入流中讀取屬性列表
properties.load(input);
// 遍歷所有鍵值對(duì)
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
System.out.println(key + ": " + value);
}
} catch (IOException e) {
e.printStackTrace();
}
5. store(OutputStream out, String comments)
Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
try (OutputStream output = new FileOutputStream("output.properties")) {
// 將屬性列表寫(xiě)入輸出流
properties.store(output, "Comments for the output file");
} catch (IOException e) {
e.printStackTrace();
}
6. store(Writer writer, String comments)
Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
try (Writer writer = new FileWriter("output.properties")) {
// 將屬性列表寫(xiě)入輸出字符流
properties.store(writer, "Comments for the output file");
} catch (IOException e) {
e.printStackTrace();
}
3. Demo
上述的API方法可適當(dāng)選擇,完整的Demo可充分了解這個(gè)類的整體邏輯:
import java.io.*;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
// 創(chuàng)建 Properties 對(duì)象
Properties properties = new Properties();
// 設(shè)置屬性值
properties.setProperty("username", "碼農(nóng)研究僧");
properties.setProperty("password", "123456789");
properties.setProperty("server", "https://blog.csdn.net/weixin_47872288");
// 將屬性列表寫(xiě)入輸出流
try (OutputStream output = new FileOutputStream("config.properties")) {
properties.store(output, "Sample Configuration");
System.out.println("Properties written to config.properties");
} catch (IOException e) {
e.printStackTrace();
}
// 從輸入流中讀取屬性列表
try (InputStream input = new FileInputStream("config.properties")) {
properties.load(input);
// 獲取屬性值
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String server = properties.getProperty("server");
System.out.println("Username: " + username);
System.out.println("Password: " + password);
System.out.println("Server: " + server);
} catch (IOException e) {
e.printStackTrace();
}
// 打印屬性列表到控制臺(tái)
/**
* -- listing properties --
* password=123456789
* server=https://blog.csdn.net/weixin_47872288
* username=碼農(nóng)研究僧
*/
properties.list(System.out);
}
}
終端輸出結(jié)果如下:

到此這篇關(guān)于深入理解Java中的Properties類的文章就介紹到這了,更多相關(guān)Java Properties類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA不可變類(immutable)機(jī)制與String的不可變性(推薦)
這篇文章主要介紹了JAVA不可變類(immutable)機(jī)制與String的不可變性(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
java構(gòu)造http請(qǐng)求的幾種方式(附源碼)
本文主要介紹了java構(gòu)造http請(qǐng)求的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Spring的初始化和XML解析的實(shí)現(xiàn)
這篇文章主要介紹了Spring的初始化和XML解析的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Java多線程Runable售票系統(tǒng)實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Java多線程Runable售票系統(tǒng)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

