java 多个类共用属性_java中读写Properties属性文件公用方法详解
前言
大家都知道Java中有個(gè)比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語(yǔ)言都有自己所支持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過(guò)該類的方法來(lái)修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內(nèi)容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來(lái)注釋。
它提供了幾個(gè)主要的方法:
1. getProperty ( String key) , 用指定的鍵在此屬性列表中搜索屬性。也就是通過(guò)參數(shù) key ,得到 key 所對(duì)應(yīng)的 value。
2. load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對(duì))。通過(guò)對(duì)指定的文件(比如說(shuō)上面的 test.properties 文件)進(jìn)行裝載來(lái)獲取該文件中的所有鍵 - 值對(duì)。以供 getProperty ( String key)來(lái)搜索。
3. setProperty ( String key, String value) ,調(diào)用 Hashtable 的方法 put 。他通過(guò)調(diào)用基類的put方法來(lái)設(shè)置 鍵 - 值對(duì)。
4. store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對(duì))寫(xiě)入輸出流。與 load 方法相反,該方法將鍵 - 值對(duì)寫(xiě)入到指定的文件中去。
5. clear (),清除所有裝載的 鍵 - 值對(duì)。該方法在基類中提供。
如下示例代碼提供了一套讀寫(xiě)配置文件的公用實(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 {
// 注意路徑以 / 開(kāi)始,沒(méi)有則處理
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;
}
}
/**
* 打印配置文件全部?jī)?nèi)容(filePath,配置文件名,如果有路徑,props/test.properties)
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
// 注意路徑以 / 開(kāi)始,沒(méi)有則處理
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);
}
}
/**
* 將值寫(xiě)入配置文件
*/
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
// 本地測(cè)試特別注意,如果是maven項(xiàng)目,請(qǐ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讀寫(xiě)Properties屬性文件公用方法的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
總結(jié)
以上是生活随笔為你收集整理的java 多个类共用属性_java中读写Properties属性文件公用方法详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在家网上兼职工作(12 个最佳兼职在家工
- 下一篇: 如何设置允许空密码帐户远程登录电脑电脑如