java中讀寫Properties屬性文件公用辦法詳解。本站提示廣大學習愛好者:(java中讀寫Properties屬性文件公用辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是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屬性文件公用辦法的全部內容了,希望本文的內容對大家的學習或許任務能帶來一定的協助,假如有疑問大家可以留言交流。