深入理解Java中的Properties類
前言
使用Properties出現(xiàn)中文亂碼可看我這篇文章:properties出現(xiàn)中文亂碼解決方法(萬能)
1. 基本知識(shí)
Properties 類是 Java 中用于處理配置文件的工具類,它繼承自 Hashtable 類,實(shí)現(xiàn)了 Map 接口。
- 主要用于讀取和寫入屬性文件,以鍵值對的形式存儲(chǔ)數(shù)據(jù)。
- 配置文件通常以 .properties 為擴(kuò)展名,其中每一行表示一個(gè)屬性或配置項(xiàng)。
使用該類可以更好的處理文件:
- 配置文件管理: 主要用于讀取和保存應(yīng)用程序的配置信息,例如數(shù)據(jù)庫連接信息、用戶設(shè)置等。
- 國際化: 用于加載不同語言的資源文件,方便國際化應(yīng)用程序。
- 持久化: 可以將配置信息持久化到文件,以便下次程序啟動(dòng)時(shí)重新加載。
對于其方法可查看其API接口:Properties API接口
構(gòu)造方法如下:
| 方法 | 表述 |
|---|---|
| Properties() | 創(chuàng)建一個(gè)沒有默認(rèn)值的空屬性列表。 |
| Properties?(int initialCapacity) | 創(chuàng)建一個(gè)沒有默認(rèn)值的空屬性列表,并且初始大小容納指定數(shù)量的元素,而無需動(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é)流中讀取屬性列表(鍵和元素對)。 |
| void | load?(Reader reader) | 以簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。 |
| Object | setProperty?(String key, String value) | 調(diào)用 Hashtable方法 put 。 |
| void | store?(OutputStream out, String comments) | 將此 Properties表中的此屬性列表(鍵和元素對)以適合使用 load(InputStream)方法加載到 Properties表的格式寫入輸出流。 |
| void | store?(Writer writer, String comments) | 將此 Properties表中的此屬性列表(鍵和元素對)以適合使用 load(Reader)方法的格式寫入輸出字符流。 |
2. 代碼示例
當(dāng)使用 Properties 類時(shí),你可以使用上述方法來讀取和寫入屬性。以下是這些方法的一些簡單的代碼示例:
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);
// 遍歷所有鍵值對
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")) {
// 將屬性列表寫入輸出流
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")) {
// 將屬性列表寫入輸出字符流
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 對象
Properties properties = new Properties();
// 設(shè)置屬性值
properties.setProperty("username", "碼農(nóng)研究僧");
properties.setProperty("password", "123456789");
properties.setProperty("server", "https://blog.csdn.net/weixin_47872288");
// 將屬性列表寫入輸出流
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java RandomAccessFile 指定位置實(shí)現(xiàn)文件讀取與寫入
這篇文章主要介紹了Java RandomAccessFile 指定位置實(shí)現(xiàn)文件讀取與寫入的相關(guān)資料,需要的朋友可以參考下2017-01-01
maven倉庫repositories和mirrors的配置及區(qū)別詳解
這篇文章主要介紹了maven倉庫repositories和mirrors的配置及區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot整合RocketMq實(shí)現(xiàn)分布式事務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合RocketMq實(shí)現(xiàn)分布式事務(wù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-11-11
詳解Java異常處理中throw與throws關(guān)鍵字的用法區(qū)別
這篇文章主要介紹了詳解Java異常處理中throw與throws關(guān)鍵字的用法區(qū)別,這也是Java面試題目中的常客,需要的朋友可以參考下2015-11-11
深入理解Java設(shè)計(jì)模式之職責(zé)鏈模式
這篇文章主要介紹了JAVA設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解2021-11-11
mybatis+springboot中使用mysql的實(shí)例
在軟件開發(fā)中,數(shù)據(jù)庫的引入是必不可少的,這里來展現(xiàn)一下通過mybatis框架在springboot中使用mysql,具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07
java 如何給對象中的包裝類設(shè)置默認(rèn)值
這篇文章主要介紹了java 如何給對象中的包裝類設(shè)置默認(rèn)值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

