java 读取项目外面配置文件的方法

public static void loadProps(String propertiesUrl) {
    props = new Properties();
    InputStream in = null;
    try {
        in =new BufferedInputStream(new FileInputStream(propertiesUrl));
        props.load(in);
    } catch (FileNotFoundException e) {
        logger.error("jdbc.properties文件未找到");
    } catch (IOException e) {
        logger.error("出现IOException");
    } finally {
        try {
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("jdbc.properties文件流关闭出现异常");
        }
    }
    logger.debug("加载properties文件内容完成...........");
    logger.debug("properties文件内容:" + props);
}

public static String getProperty(String key) {
if (null == props) {
logger.debug("加载配置文件出错");
}
return props.getProperty(key);
}

原文地址:https://www.cnblogs.com/jpfss/p/9790185.html