阅读背景:

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);
    }
}package com.cnblog.util;

i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: