读取Properties配置文件的工具类

以下为读取Properties配置文件的工具类:

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @author : ShiLei
 * @time :2018年1月2日 下午5:33:51
 * @introduction :  Properties配置文件操作:利用国际化资源文件类ResourceBundle来读取资源属性文件(properties).
 */
public class PropertiesUtils {
	
    //要Locale 来执行其任务的操作称为语言环境敏感的操作.
	public static Locale locale = new Locale("zh", "CN");
	//public static Locale locale = new Locale("en", "US");
	
	/**
	 * 取得指定properties文件的指定key的value
	 * 
	 * @param fileName:properties文件的路径及名称(不含扩展名)
	 *            
	 * @param key:所指定的key
	 *            
	 * @return value:指定key对应的value值
	 * @throws MissingResourceException:当没找到该指定的properties文件,或该文件中指定的key不存在时.
	 *             
	 */
	public static String getValue(String fileName, String key) throws MissingResourceException{
		final ResourceBundle res = ResourceBundle.getBundle(fileName,locale);
		String value = res.getString(key);
		return value.trim();
	}

	/**
	 * 将文件中配置信息转换到properties对象中
	 * 
	 * @param fileName: properties文件的路径及名称(不含扩展名)
	 * @return Properties对象
	 */
	public static Properties toProperties(String fileName) throws MissingResourceException{
		Properties properties = new Properties();
		final ResourceBundle res = ResourceBundle.getBundle(fileName,locale);
		Enumeration<String> en = res.getKeys();
		String key = null;
		String value = null;
		while (en.hasMoreElements()){
			key = en.nextElement().trim();
			value = res.getString(key);
			properties.setProperty(key, value.trim());
		}
		return properties;
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值