加载properties文件 文件路径

public class PropertyUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    
    private static Properties props;
    static{
        loadProps();
    }


    synchronized static private void loadProps(){
        logger.info("开始加载properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
            in = PropertyUtil.class.getClassLoader().getResourceAsStream("httpUrl.properties");
            props.load(in);
        } catch (FileNotFoundException e) {
            logger.error("httpUrl.properties文件未找到", e);
        } catch (IOException e) {
            logger.error("出现IOException", e);
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("httpUrl.properties文件流关闭出现异常", e);
            }
        }
        logger.info("加载properties文件内容完成...........");
        logger.info("properties文件内容:" + props);
    }

   //根据key获得内容
    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }


    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
}

相关文章:

  • 2022-01-16
  • 2021-11-20
  • 2021-06-21
  • 2021-06-27
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-22
  • 2021-10-01
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
相关资源
相似解决方案