天天看點

java 中擷取配置檔案的值_Java讀取Properties配置檔案

1.Properties類與Properties配置檔案

Properties類繼承自Hashtable類并且實作了Map接口,使用鍵值對的形式來儲存屬性集。不過Properties的鍵和值都是字元串類型。

2.Properties中的主要方法

(1)load(InputStream inStream)

此方法可以從.properties屬性檔案對應的檔案輸入流中,加載屬性清單到Properties類對象。用于讀取Properties配置檔案。Properties prop = new Properties();

//讀取屬性檔案a.properties

InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));

prop.load(in);     ///加載屬性清單

Iterator it=prop.stringPropertyNames().iterator();

while(it.hasNext()){

String key=it.next();

System.out.println(key+":"+prop.getProperty(key));

}

in.close();

(2)store(OutputStream out, String comments)

此方法将Properties類對象的屬性清單儲存到輸出流中。用于寫Properties配置檔案。Properties prop = new Properties();

//儲存屬性到b.properties檔案

FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打開

prop.setProperty("phone", "10086");

prop.store(oFile, "Comment");//如果comments不為空,儲存後的屬性檔案第一行會是#comments,表示注釋資訊;如果為空則沒有注釋資訊。注釋資訊後面是屬性檔案的目前儲存時間資訊。

oFile.close();

(3)getProperty/setProperty

這兩個方法是分别是擷取和設定屬性資訊。

3.示例

下面是黃勇老師寫的擷取屬性檔案的工具類。

public final class PropsUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);

public static Properties loadProps(String fileName) {

Properties props = null;

InputStream is = null;

try {

is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);

if (is == null) {

throw new FileNotFoundException(fileName + " file is not found");

}

props = new Properties();

props.load(is);

} catch (IOException e) {

LOGGER.error("load properties file failure", e);

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

LOGGER.error("close input stream failure", e);

}

}

}

return props;

}

public static String getString(Properties props, String key) {

return getString(props, key, "");

}

public static String getString(Properties props, String key, String defaultValue) {

String value = defaultValue;

if (props.containsKey(key)) {

value = props.getProperty(key);

}

return value;

}

public static int getInt(Properties props, String key) {

return getInt(props, key, 0);

}

// 擷取數值型屬性(可指定預設值)

public static int getInt(Properties props, String key, int defaultValue) {

int value = defaultValue;

if (props.containsKey(key)) {

value = CastUtil.castInt(props.getProperty(key));

}

return value;

}

public static boolean getBoolean(Properties props, String key) {

return getBoolean(props, key, false);

}

public static boolean getBoolean(Properties props, String key, boolean defaultValue) {

boolean value = defaultValue;

if (props.containsKey(key)) {

value = CastUtil.castBoolean(props.getProperty(key));

}

return value;

}

}

裡面用到了CastUtil類,該類是為了處理一些資料轉型操作而準備的,代碼如下:

public final class CastUtil {

public static String castString(Object obj) {

return CastUtil.castString(obj, "");

}

public static String castString(Object obj, String defaultValue) {

return obj != null ? String.valueOf(obj) : defaultValue;

}

public static double castDouble(Object obj) {

return CastUtil.castDouble(obj, 0);

}

public static double castDouble(Object obj, double defaultValue) {

double doubleValue = defaultValue;

if (obj != null) {

String strValue = castString(obj);

if (StringUtil.isNotEmpty(strValue)) {

try {

doubleValue = Double.parseDouble(strValue);

} catch (NumberFormatException e) {

doubleValue = defaultValue;

}

}

}

return doubleValue;

}

public static long castLong(Object obj) {

return CastUtil.castLong(obj, 0);

}

public static long castLong(Object obj, long defaultValue) {

long longValue = defaultValue;

if (obj != null) {

String strValue = castString(obj);

if (StringUtil.isNotEmpty(strValue)) {

try {

longValue = Long.parseLong(strValue);

} catch (NumberFormatException e) {

longValue = defaultValue;

}

}

}

return longValue;

}

public static int castInt(Object obj) {

return CastUtil.castInt(obj, 0);

}

public static int castInt(Object obj, int defaultValue) {

int intValue = defaultValue;

if (obj != null) {

String strValue = castString(obj);

if (StringUtil.isNotEmpty(strValue)) {

try {

intValue = Integer.parseInt(strValue);

} catch (NumberFormatException e) {

intValue = defaultValue;

}

}

}

return intValue;

}

public static boolean castBoolean(Object obj) {

return CastUtil.castBoolean(obj, false);

}

public static boolean castBoolean(Object obj, boolean defaultValue) {

boolean booleanValue = defaultValue;

if (obj != null) {

booleanValue = Boolean.parseBoolean(castString(obj));

}

return booleanValue;

}

}

castUtil類中用到了StringUtil類,它提供一些字元串操作,代碼如下:

public final class StringUtil {

public static boolean isEmpty(String str) {

if (str != null) {

str = str.trim();

}

return StringUtils.isEmpty(str);

}

public static boolean isNotEmpty(String str) {

return !isEmpty(str);

}

}

這樣就可以直接運用這個工具類操作Properties配置檔案了。

操作示例(擷取一個int屬性的值,并給一個預設值1):Properties conf = PropsUtil.loadProps("config.properties");

int value = PropsUtil.getInt(conf,"key",1);

這樣操作就友善多了。