Java工具类(三) 解析配置文件工具类

解析properties文件工具类

package com.cnblog.util;

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

/**
 * 解析配置文件工具类
 * @author jian.liu
 */
public class ParseFileUtil {
	/**
	 * 根据key解析value为英文的properties文件
	 * @param path 文件路径 列如:要想读取conn.properties,path的值为conn.properties
	 * @param key 键值
	 * @return
	 */
	public static String parse(String path, String key) {
		Properties prop = new Properties();
		try {
                            prop.load(ParseFileUtil.class.getClassLoader().getResourceAsStream(path));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException( "没有找到该路径下对应的文件" +e);
		}
		return prop.getProperty(key);
	}
	
	/**
	 * 根据key解析value为中文的properties文件 
	 * @param path 文件路径 列如:要想读取conn.properties,path的值为conn.properties
	 * @param key 键值
	 * @return
	 */
	public static String parseChinese(String path, String key) {
		Properties properties = new Properties();
		InputStream inputStream = ParseFileUtil.class.getResourceAsStream("/" + path);
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			properties.load(bf);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException( "没有找到该路径下对应的文件" +e);
		}
		return properties.getProperty(key);
	}
}
posted @ 2015-09-27 21:38  ljeabbk  阅读(442)  评论(0编辑  收藏  举报