辅助类之读取Properties文件封装类

开发环境说明:JDK1.5

package biz.bebig.management.server.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Load configurations from property file in classpath, default is
 * {@link #PROPERTY_FILE}.
 * @author tangxiaochun88#gmail.com
 *
 */

public class Config
{
    private static final Logger log = LoggerFactory.make();
    /**
     * Default property file for the configuration of the clustering updater
     */
    public static final String PROPERTY_FILE = "default.properties";

    private Properties prop;

    /**
     * Default Constructor using the default {@link #PROPERTY_FILE}.
     */
    public Config()
    {
        this(PROPERTY_FILE);
    }

    /**
     * Constructor that takes a custom property file.
     * 
     * @param propertyFile
     *            custom property file in classpath
     */
    public Config(String propertyFile)
    {
        prop = new Properties();
        try
        {
            InputStream stream = getClass().getClassLoader()
                    .getResourceAsStream(propertyFile);
            if (stream == null)
                stream = ClassLoader.getSystemResourceAsStream(propertyFile);
            prop.load(stream);
        } catch (IOException e)
        {
            log.error("Cannot load config file " + PROPERTY_FILE, e);
        }
    }

    /**
     * Get the property value of the given property key
     * 
     * @param key
     *            property key
     * @return value of the property
     */
    public String getProperty(String key)
    {
        return prop.getProperty(key);
    }

    public String getPropertyNotEmpty(String key)
    {
        String value = getProperty(key);
        if (StringUtils.isBlank(value))
            throw new RuntimeException("property " + key + " is blank");
        return value;
    }

    /**
     * Get the boolean value of the given property key
     * 
     * @param key
     *            property key
     * @return true/false
     */
    public boolean getBooleanProperty(String key)
    {
        String value = getProperty(key);
        if ("true".equalsIgnoreCase(value))
            return true;
        if ("false".equalsIgnoreCase(value))
            return false;
        throw new RuntimeException("value of property " + key
                + " must be either true/false");
    }

    public boolean getBooleanPropertyNotEmpty(String key)
    {
        String value = getPropertyNotEmpty(key);
        if ("true".equalsIgnoreCase(value))
            return true;
        if ("false".equalsIgnoreCase(value))
            return false;
        throw new RuntimeException("value of property " + key
                + " must be either true/false");
    }

    /**
     * Get the integer value of the given property key
     * 
     * @param key
     *            property key
     * @return integer value of the property
     */
    public int getIntProperty(String key)
    {
        String value = getProperty(key);
        return Integer.parseInt(value);
    }

    public int getIntPropertyNotEmpty(String key)
    {
        String value = getPropertyNotEmpty(key);
        return Integer.parseInt(value);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值