Properties屬性文件操作Java工具類

peke 10年前發布 | 3K 次閱讀 Java

public class PropsUtil {

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

private Properties props = null;

public PropsUtil(String propsPath) {
    this(propsPath, Const.DEFAULT_ENCODING);
}

public PropsUtil(String propsPath, String encoding) {
    InputStream is = null;
    try {
        if (StrUtil.isBlank(propsPath)) {
            throw new IllegalArgumentException();
        }
        String suffix = ".properties";
        if (propsPath.lastIndexOf(suffix) == -1) {
            propsPath += suffix;
        }
        is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsPath);
        if (is != null) {
            props = new Properties();
            props.load(new InputStreamReader(is, encoding));
        }
    } catch (Exception e) {
        logger.error("加載屬性文件出錯!", e);
        throw new RuntimeException(e);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            logger.error("釋放資源出錯!", e);
        }
    }
}

/**
 * 加載屬性文件,并轉為 Map
 */
public Map<String, String> loadPropsToMap(String propsPath) {
    Map<String, String> map = new HashMap<String, String>();
    for (String key : props.stringPropertyNames()) {
        map.put(key, props.getProperty(key));
    }
    return map;
}

/**
 * 獲取字符型屬性
 */
public String getString(String key) {
    return props.getProperty(key);
}

/**
 * 獲取字符型屬性,有默認值
 */
public String getString(String key, String defaultValue) {
    return props.getProperty(key, defaultValue);
}

/**
 * 獲取字符型屬性(有默認值)
 */
public static String getString(Properties props, String key, String defalutValue) {
    String value = defalutValue;
    if (props.containsKey(key)) {
        value = props.getProperty(key);
    }
    return value;
}

/**
 * 獲取數值型屬性
 */
public Integer getInt(String key) {
    return getInt(key, null);
}
public Integer getInt(String key, Integer defaultValue) {
    String value = props.getProperty(key);
    if (value != null)
        return Integer.parseInt(value.trim());
    return defaultValue;
}

/**
 * 獲取Long型
 */
public Long getLong(String key) {
    return getLong(key, null);
}
public Long getLong(String key, Long defaultValue) {
    String value = props.getProperty(key);
    if (value != null)
        return Long.parseLong(value.trim());
    return defaultValue;
}

/**
 * 獲取布爾型屬性
 */
public Boolean getBoolean(String key) {
    return getBoolean(key, null);
}
public Boolean getBoolean(String key, Boolean defaultValue) {
    String value = props.getProperty(key);
    if (value != null) {
        value = value.toLowerCase().trim();
        if ("true".equals(value))
            return true;
        else if ("false".equals(value))
            return false;
        throw new RuntimeException("The value can not parse to Boolean : " + value);
    }
    return defaultValue;
}

public boolean containsKey(String key) {
    return props.containsKey(key);
}

public Properties getProperties() {
    return props;
}

}</pre>

 本文由用戶 peke 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!