java中讀寫Properties屬性文件公用方法詳解
前言
大家都知道Java中有個比較重要的類Properties(Java.util.Properties
),主要用于讀取Java的配置文件,各種語言都有自己所支持的配置文件,配置文件中很多變量是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關的變量設置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。
它提供了幾個主要的方法:
1. getProperty ( String key)
, 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value。
2. load ( InputStream inStream)
,從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key)
來搜索。
3. setProperty ( String key, String value)
,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。
4. store ( OutputStream out, String comments)
,以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。
5. clear ()
,清除所有裝載的 鍵 - 值對。該方法在基類中提供。
如下示例代碼提供了一套讀寫配置文件的公用實用方法,可以根據自己的項目進行引入:
package com.javacui.lucene.jdbc; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Properties; import org.apache.log4j.Logger; public class PropertieUtil { private static Logger logger = Logger.getLogger(PropertieUtil.class); private PropertieUtil() { } /** * 讀取配置文件某屬性 */ public static String readValue(String filePath, String key) { Properties props = new Properties(); try { // 注意路徑以 / 開始,沒有則處理 if (!filePath.startsWith("/")) filePath = "/" + filePath; InputStream in = PropertieUtil.class.getResourceAsStream(filePath); props.load(in); String value = props.getProperty(key); return value; } catch (Exception e) { logger.error(e); return null; } } /** * 打印配置文件全部內容(filePath,配置文件名,如果有路徑,props/test.properties) */ public static void readProperties(String filePath) { Properties props = new Properties(); try { // 注意路徑以 / 開始,沒有則處理 if (!filePath.startsWith("/")) filePath = "/" + filePath; InputStream in = PropertieUtil.class.getResourceAsStream(filePath); props.load(in); Enumeration<?> en = props.propertyNames(); // 遍歷打印 while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key); logger.info(key + ":" + Property); } } catch (Exception e) { logger.error(e); } } /** * 將值寫入配置文件 */ public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception { // 本地測試特別注意,如果是maven項目,請到\target目錄下查看文件,而不是源代碼下 // 注意路徑不能加 / 了,加了則移除掉 if (fileName.startsWith("/")) fileName.substring(1); String filePath = PropertieUtil.class.getResource("/").getPath()+fileName; // 獲取配置文件 Properties pps = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream(filePath)); pps.load(in); in.close(); OutputStream out = new FileOutputStream(filePath); // 設置配置名稱和值 pps.setProperty(parameterName, parameterValue); // comments 等于配置文件的注釋 pps.store(out, "Update " + parameterName + " name"); out.flush(); out.close(); } public static void main(String[] args) throws Exception { readProperties("jdbc.properties"); // logger.info(readValue("jdbc.properties", "JAVABLOG_WRITE_URL")); // writeProperties("test.properties", "test", "test"); } }
總結
以上就是關于java讀寫Properties屬性文件公用方法的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
springboot整合Dubbo與Feign的實現?(無注冊中心)
本文主要介紹了springboot整合Dubbo與Feign的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Spring Boot 編寫Servlet、Filter、Listener、Interceptor的方法
這篇文章給大家介紹了spring-boot中如何定義過濾器、監(jiān)聽器和攔截器,對Spring Boot 編寫Servlet、Filter、Listener、Interceptor的相關知識感興趣的朋友一起看看吧2017-07-07基于synchronized修飾靜態(tài)和非靜態(tài)方法
這篇文章主要介紹了基于synchronized修飾靜態(tài)和非靜態(tài)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04