读取配置工具类

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Sam
 * @Description: 读取配置工具类
 * @date 2018/5/23
 */
public class PropertiesUtils {
    private static Properties props;

    static {
        loadProps();
    }

    synchronized static private void loadProps() {
        props = new Properties();
        InputStream in = null;
        try {
            // 要加载的属性文件
            in = PropertiesUtils.class.getResourceAsStream("/config.properties");
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    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);
    }
}

猜你喜欢

转载自blog.csdn.net/grxasd/article/details/81018042
今日推荐