java 获取系统资源 properties

/**
 * The Class SystemResource.
 */
public final class SystemResource {

    /** The properties. */
    private Properties            properties = null;

    /** The Constant FILENAME. */
    public static final String    FILENAME   = "properties/sys-resource.properties";

    /** The instance. */
    private static SystemResource instance   = null;

    /**
     * Instantiates a new system resource.
     */
    private SystemResource() {
        try {
            readProperties(FILENAME);
        } catch (AdsSystemException e) {
            AdsLogger.error(e);
        }
    }

    /**
     * Gets the single instance of SystemResource.
     *
     * @return single instance of SystemResource
     */
    public static SystemResource getInstance() {

        if (instance == null) {
            instance = new SystemResource();
        }
        return instance;
    }

    /**
     * Get.
     *
     * @param key the key
     * @return the string
     */
    public String get(String key) {

        if (properties.containsKey(key)) {
            return properties.getProperty(key);
        }

        return null;
    }

    /**
     * Read properties.
     *
     * @param file the file
     * @throws AdsSystemException the ads system exception
     */
    protected void readProperties(String file) throws AdsSystemException {

        // 1. init?
        if (properties != null) {
            return;
        }

        // 3. first time
        properties = new Properties();

        // 4. read
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);

        // 6. load file
        try {
            properties.load(in);
        } catch (IOException e) {
            throw new AdsSystemException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new AdsSystemException(e);
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值