java读取配置文件工具类

package com.safein.util;

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

import org.apache.log4j.Logger;


/**
 * 公共配置文件读取操作类
 * @author julong
 * @date 2016年5月10日 上午9:35:40
 */
public class MessageUtil {
	//日志
	private static final Logger logger = Logger.getLogger(MessageUtil.class);
	//当前对象
	private static MessageUtil instance = null;
	private Properties properties = null;
	/**
	 * 配置文件
	 * @author julong
	 * @date 2016-7-25 上午11:46:27
	 */
	private static final String configFile = "message.properties";
	
	/**
	 * 单例化对象
	 * @return
	 * @author julong
	 * @date 2016年5月11日 上午11:50:48
	 */
	public static synchronized MessageUtil getInstance() {
		if (null == instance) {
			instance = new MessageUtil();
		}
		return instance;
	}
	
	/**
	 * 私有化构造函数
	 * @author julong
	 * @date 2016年5月11日 上午11:50:26
	 */
	private MessageUtil() {
		logger.debug("Init the config properties...");
		properties = new Properties();
		InputStream is = null;
		try {
			is = this.getClass().getClassLoader().getResourceAsStream(configFile);
			properties.load(is);
		} catch (IOException e) {
			throw new RuntimeException("返回码配置文件读取失败");
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭返回码配置文件读取流失败");
				}
			}
		}
		logger.debug("Finish read config properties.");
	}
	
	/**
	 * 获取当前是否存在键值对
	 * @param key
	 * @return
	 * @author julong
	 * @date 2016年5月10日 上午9:39:37
	 */
	public boolean containsKey(String key) {
		return properties.containsKey(key);
	}

	/**
	 * 获取key对应的value
	 * @param retCode
	 * @return
	 * @author julong
	 * @date 2016年5月10日 上午9:40:11
	 */
	public String getProperty(String key) {
		return properties.getProperty(key);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MessageUtil configProperties = new MessageUtil();
		boolean result = configProperties.containsKey("000000");
		System.out.println(result);
		String value = configProperties.getProperty("000000");
		System.out.println(value);
	}

}



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

import org.apache.log4j.Logger;


/**
 * 公共配置文件读取操作类
 * @author julong
 * @date 2016年5月10日 上午9:35:40
 */
public class MessageUtil {
	//日志
	private static final Logger logger = Logger.getLogger(MessageUtil.class);
	//当前对象
	private static MessageUtil instance = null;
	private Properties properties = null;
	/**
	 * 配置文件
	 * @author julong
	 * @date 2016-7-25 上午11:46:27
	 */
	private static final String configFile = "message.properties";
	
	/**
	 * 单例化对象
	 * @return
	 * @author julong
	 * @date 2016年5月11日 上午11:50:48
	 */
	public static synchronized MessageUtil getInstance() {
		if (null == instance) {
			instance = new MessageUtil();
		}
		return instance;
	}
	
	/**
	 * 私有化构造函数
	 * @author julong
	 * @date 2016年5月11日 上午11:50:26
	 */
	private MessageUtil() {
		logger.debug("Init the config properties...");
		properties = new Properties();
		InputStream is = null;
		try {
			is = this.getClass().getClassLoader().getResourceAsStream(configFile);
			properties.load(is);
		} catch (IOException e) {
			throw new RuntimeException("返回码配置文件读取失败");
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					throw new RuntimeException("关闭返回码配置文件读取流失败");
				}
			}
		}
		logger.debug("Finish read config properties.");
	}
	
	/**
	 * 获取当前是否存在键值对
	 * @param key
	 * @return
	 * @author julong
	 * @date 2016年5月10日 上午9:39:37
	 */
	public boolean containsKey(String key) {
		return properties.containsKey(key);
	}

	/**
	 * 获取key对应的value
	 * @param retCode
	 * @return
	 * @author julong
	 * @date 2016年5月10日 上午9:40:11
	 */
	public String getProperty(String key) {
		return properties.getProperty(key);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MessageUtil configProperties = new MessageUtil();
		boolean result = configProperties.containsKey("000000");
		System.out.println(result);
		String value = configProperties.getProperty("000000");
		System.out.println(value);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值