读取properties配置文件工具类

来源:互联网 发布:python量化交易 编辑:程序博客网 时间:2024/06/03 20:40
package com.ule.util;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.Properties;public class PropertiesUtils {    /**     * (注意:加载的是src下的文件,如果在某个包下.请把包名加上)     * @return     */    public static Properties getProperties(String sourcesFileName){        Properties properties = new Properties();        String savePath = Thread.currentThread().getContextClassLoader().getResource(sourcesFileName).getPath();        try{            InputStream inputStream = new BufferedInputStream(new FileInputStream(savePath));            properties.load(inputStream);        }catch (Exception e){            e.printStackTrace();        }        return  properties;    }    /**     * 获取属性文件的数据 根据key获取值     * @param key     * @return     */    public static String findPorpertyByKey(String key,String sourcesFileName){        try {            Properties pro = getProperties(sourcesFileName);            return pro.getProperty(key);        }catch (Exception e){            return "";        }    }}
0 0
原创粉丝点击