Properties 工具类

Properties 工具类
/**
 * Program  : PropertyConfigUtil.java<br/>
 * Author   : Ices<br/>
 * Create   : 2007-7-24 上午10:46:57<br/>
 *
 * Copyright 1997-2006 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").
 * You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement
 * you entered into with Embedded Internet Solutions Inc.
 *
 */


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

import org.apache.log4j.Logger;

public class PropertyConfigUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PropertyConfigUtil.class);

	private String propertiesPath;

	
	public PropertyConfigUtil(String propertiesPath) {
		this.propertiesPath = propertiesPath;
	}

	public String getValue(String key) {
		Properties properties = new Properties();
		InputStream inputStream = null;
		try {
			inputStream = PropertyConfigUtil.class
					.getResourceAsStream(this.propertiesPath);
			properties.load(inputStream);
			//check(key, properties);
			return properties.getProperty(key);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.warn("getValue(String) - exception ignored", e);
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("getValue(String)", e);
		} finally {
			try {
				if (inputStream == null) {
					logger.error("can not get resourse:" + propertiesPath);
				} else {
					inputStream.close();
				}
			} catch (IOException e) {
				logger.error("getValue(String)", e);
				e.printStackTrace();
			}
		}
		return null;
	}

	protected boolean check(String key, Properties properties) {
		if (properties.containsKey(key)) {
			return true;
		} else {
			throw new IllegalArgumentException("the property file["
					+ this.propertiesPath + "] do not have the key:" + key);
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值