java中讀寫Properties屬性文件公用方法詳解
前言
大家都知道Java中有個(gè)比較重要的類Properties(Java.util.Properties
),主要用于讀取Java的配置文件,各種語言都有自己所支持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內(nèi)容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。
它提供了幾個(gè)主要的方法:
1. getProperty ( String key)
, 用指定的鍵在此屬性列表中搜索屬性。也就是通過參數(shù) key ,得到 key 所對應(yīng)的 value。
2. load ( InputStream inStream)
,從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進(jìn)行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key)
來搜索。
3. setProperty ( String key, String value)
,調(diào)用 Hashtable 的方法 put 。他通過調(diào)用基類的put方法來設(shè)置 鍵 - 值對。
4. store ( OutputStream out, String comments)
,以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。
5. clear ()
,清除所有裝載的 鍵 - 值對。該方法在基類中提供。
如下示例代碼提供了一套讀寫配置文件的公用實(shí)用方法,可以根據(jù)自己的項(xiàng)目進(jìn)行引入:
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; } } /** * 打印配置文件全部內(nèi)容(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項(xiàng)目,請到\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); // 設(shè)置配置名稱和值 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"); } }
總結(jié)
以上就是關(guān)于java讀寫Properties屬性文件公用方法的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
java MyBatis攔截器Inteceptor詳細(xì)介紹
這篇文章主要介紹了java MyBatis攔截器Inteceptor詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Java如何根據(jù)實(shí)體指定字段值對其List進(jìn)行排序詳解
在Java項(xiàng)目中可能會(huì)遇到給出一些條件,將List元素按照給定條件進(jìn)行排序的情況,這篇文章主要給大家介紹了關(guān)于Java如何根據(jù)實(shí)體指定字段值對其List進(jìn)行排序的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07Java注冊郵箱激活驗(yàn)證實(shí)現(xiàn)代碼
這篇文章主要介紹了Java注冊郵箱激活驗(yàn)證實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12springboot整合Dubbo與Feign的實(shí)現(xiàn)?(無注冊中心)
本文主要介紹了springboot整合Dubbo與Feign的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring Boot 編寫Servlet、Filter、Listener、Interceptor的方法
這篇文章給大家介紹了spring-boot中如何定義過濾器、監(jiān)聽器和攔截器,對Spring Boot 編寫Servlet、Filter、Listener、Interceptor的相關(guān)知識感興趣的朋友一起看看吧2017-07-07基于synchronized修飾靜態(tài)和非靜態(tài)方法
這篇文章主要介紹了基于synchronized修飾靜態(tài)和非靜態(tài)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java Array和Arrays的區(qū)別總結(jié)
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java Array和Arrays的區(qū)別總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-03-03