zoukankan      html  css  js  c++  java
  • 配置相关的一些辅助类

    package com.opslab.util;

    import org.apache.log4j.Logger;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.List;
    import java.util.Properties;

    /**
    * 配置相关的一些辅助类
    */
    public class ConfigUtil {

    private static Logger logger = Logger.getLogger(ConfigUtil.class);

    /**
    * 获取配置文件资源
    *
    * @return
    */
    public static URL findAsResource(final String path) {
    URL url = null;

    ClassLoader contextClassLoader = ClassUtil.getContextClassLoader();
    if (contextClassLoader != null) {
    url = contextClassLoader.getResource(path);
    }
    if (url != null)
    return url;

    url = ConfigUtil.class.getClassLoader().getResource(path);
    if (url != null)
    return url;

    url = ClassLoader.getSystemClassLoader().getResource(path);

    return url;
    }

    /**
    * @param path
    * @return
    */
    public static String resourcePath(final String path) {
    URL asResource = findAsResource(path);
    return new File(asResource.getFile()).getPath();
    }


    private static InputStream getConfigStream(final String path) throws RuntimeException {
    try {
    URL url = new URL(path);
    return url.openStream();
    } catch (IOException e) {
    throw new RuntimeException("Unable to open config file: " + path);
    }
    }

    /**
    * 获取资源流
    *
    * @param path
    * @return
    * @throws IOException
    */
    private static InputStream resourceStream(final String path) throws IOException {
    URL asResource = findAsResource(path);
    return asResource.openStream();
    }

    /**
    * 获取资源属性
    *
    * @param path
    * @return
    * @throws IOException
    */
    public static Properties getConfigProperties(String path) throws IOException {
    Properties properties = new Properties();
    properties.load(resourceStream(path));
    return properties;
    }


    }

  • 相关阅读:
    AtCoDeer and Election Report
    乒乓球
    dp(装箱)
    《非你莫属》面试者现场跟面试官用英文对话,一口流利的英语口语厉害了
    冬天喝酸奶,可以加热吗?冬天喝酸奶,这4个方面要注意
    滑县两个富豪打赌,一人吃了一泡狗屎
    为什么外国富豪都喜欢捐光所有财产
    发的微博有敏感词
    社交网络
    怎么把word中的文字拉细变瘦长
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10254813.html
Copyright © 2011-2022 走看看