深入理解Java中的Properties類
前言
使用Properties出現(xiàn)中文亂碼可看我這篇文章:properties出現(xiàn)中文亂碼解決方法(萬能)
1. 基本知識
Properties 類是 Java 中用于處理配置文件的工具類,它繼承自 Hashtable 類,實現(xiàn)了 Map 接口。
- 主要用于讀取和寫入屬性文件,以鍵值對的形式存儲數(shù)據(jù)。
- 配置文件通常以 .properties 為擴展名,其中每一行表示一個屬性或配置項。
使用該類可以更好的處理文件:
- 配置文件管理: 主要用于讀取和保存應(yīng)用程序的配置信息,例如數(shù)據(jù)庫連接信息、用戶設(shè)置等。
- 國際化: 用于加載不同語言的資源文件,方便國際化應(yīng)用程序。
- 持久化: 可以將配置信息持久化到文件,以便下次程序啟動時重新加載。
對于其方法可查看其API接口:Properties API接口
構(gòu)造方法如下:
方法 | 表述 |
---|---|
Properties() | 創(chuàng)建一個沒有默認值的空屬性列表。 |
Properties?(int initialCapacity) | 創(chuàng)建一個沒有默認值的空屬性列表,并且初始大小容納指定數(shù)量的元素,而無需動態(tài)調(diào)整大小。 |
Properties?(Properties defaults) | 創(chuàng)建具有指定默認值的空屬性列表。 |
常用的方法如下:
返回類型 | 方法 | 表述 |
---|---|---|
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 類時,你可以使用上述方法來讀取和寫入屬性。以下是這些方法的一些簡單的代碼示例:
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); // 獲取屬性值,如果不存在則使用默認值 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); // 打印屬性列表到控制臺 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可充分了解這個類的整體邏輯:
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(); } // 打印屬性列表到控制臺 /** * -- 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不可變類(immutable)機制與String的不可變性(推薦)
這篇文章主要介紹了JAVA不可變類(immutable)機制與String的不可變性(推薦)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08Java多線程Runable售票系統(tǒng)實現(xiàn)過程解析
這篇文章主要介紹了Java多線程Runable售票系統(tǒng)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06