前言
大家都知道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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞熱點(diǎn)
疑難解答
圖片精選