`
阿杜杜不是阿木木
  • 浏览: 20554 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

通过单例模式读取properties配置文件工具类

 
阅读更多

更新日期201050902

package com.jh.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;


public class ReadPropertiesFile {
	
	/**
	 * 私有化Properties对象
	 */
	private Properties prop;
	
	/**
	 * 通过单例模式获取ReadPropertiesFile对象
	 */
	private ReadPropertiesFile(){}
	private static ReadPropertiesFile rpf=new ReadPropertiesFile();
	public static ReadPropertiesFile getInstance(){return rpf;}
	
	/**
	 * 读取文件的路径
	 * @param url
	 * @throws UnsupportedEncodingException
	 */
	public void setPropertiesDataSource(String url) throws UnsupportedEncodingException{
		prop = new Properties();
		InputStream in  = getClass().getResourceAsStream(url);
		BufferedReader bf=new BufferedReader(new InputStreamReader(in,"utf-8"));
		try {
			prop.load(bf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过key值获取文件的int类型数据
	 * @param key
	 * @return
	 */
	public Integer getInteger(String key){
		return Integer.parseInt(prop.getProperty(key));
	}
	/**
	 * 通过key值获取文件的String类型数据
	 * @param key
	 * @return
	 */
	public String getString(String key){
		return prop.getProperty(key);
	}
	/**
	 * 通过key值获取文件的double类型数据
	 * @param key
	 * @return
	 */
	public Double getDouble(String key){
		return Double.parseDouble(prop.getProperty(key));
	}
	/**
	 * 通过key值获取文件的boolean类型数据
	 * @param key
	 * @return
	 */
	public Boolean getBoolean(String key){
		return Boolean.parseBoolean(prop.getProperty(key));
	}
	
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics