標籤:
一、全域配置的簡單 propertie 檔案實現
1 package com.testgs.utils; 2 3 import java.util.*; 4 import java.io.*; 5 6 7 public final class ARConfig { 8 9 private Properties conf = new Properties();10 private String prefix = "";11 /**12 * 全域設定檔名13 */14 public static final String GLOBAL_CONF_FILE = "/analysisReportConfig.properties";15 16 public ARConfig(String prefix) {17 this.prefix = prefix;18 loadConf();19 }20 21 /**22 * 取得屬性檔案執行個體23 * @param prefix 各資料庫連接首碼24 * @return25 */26 public synchronized static ARConfig getInstance(String prefix) {27 return new ARConfig(prefix);28 }29 30 public String getConfString(String name, String defaultValue) {31 String result = getConfString(name);32 result = (result == null) ? defaultValue : result;33 return result;34 }35 36 /**讀取配置資訊的 boolean 值37 * @param name38 * @param defaultValue39 * @return40 */41 public boolean getConfBoolean(String name, boolean defaultValue) {42 boolean result = defaultValue;43 String value = getConfString(name);44 if (value != null) {45 value = value.toLowerCase();46 result = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");47 }48 return result;49 }50 51 /**讀取配置資訊的 boolean 值,如果沒有,預設為 false52 * @param name53 * @return54 */55 public boolean getConfBoolean(String name) {56 return getConfBoolean(name, false);57 }58 59 /**60 * 讀取配置資訊的 int 值61 * @param name62 * @param defaultValue63 * @return64 */65 public int getConfigInt(String name, int defaultValue) {66 String intV = getConfString(name);67 int result = defaultValue;68 if (intV != null) {69 try {70 result = Integer.parseInt(intV.trim());71 } catch (Exception e) {72 e.printStackTrace();73 }74 }75 return result;76 }77 78 public String getConfString(String name) {79 name = this.prefix + name;80 return conf.getProperty(name);81 }82 83 protected synchronized void loadConf() {84 conf.clear();85 InputStream input = null;86 try {87 input = this.getClass().getResourceAsStream(GLOBAL_CONF_FILE);88 conf.load(input);89 } catch (IOException e) {90 throw new RuntimeException("找不到設定檔: " + GLOBAL_CONF_FILE);91 } finally {92 if (input != null)93 try {94 input.close();95 } catch (Exception closeE) {96 }97 }98 }99 }訪問 properties 檔案
更新中。。。
JAVA訪問設定檔總結