zoukankan      html  css  js  c++  java
  • 配置文件操作的工具类

    代码如下:

    public class PropertiesUtil {
    
        private static Properties properties = null;
      //通常把配置文件放在与src同级的目录下面,则可以通过配置文件名直接访问到相应的配置文件
        public static Properties loadProperties(String propertyFile) {
            try {
                properties = new Properties();
                InputStream stream = PropertiesUtil.class.getResourceAsStream(propertyFile);
                properties.load(stream);
            } catch (FileNotFoundException e) {
                throw new RuntimeException("cannot find config file");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return properties;
        }
        
        public static Properties loadProperties(FileInputStream stream) {
            try {
                properties = new Properties();
                properties.load(stream);
            } catch (FileNotFoundException e) {
                throw new RuntimeException("cannot find config file");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return properties;
        }
    
        public static String getProperty(String key) {
            if (properties == null) {
                throw new RuntimeException("shou loadProperties first");
            }
            return properties.getProperty(key);
        }
      //如果没有找到相应的值,则会赋予一个默认值
        public static String getProperty(String key, String defaultValue) {
            if (properties == null) {
                throw new RuntimeException("shou loadProperties first");
            }
            return properties.getProperty(key, defaultValue);
        }
    
    }
  • 相关阅读:
    python生成随机密码
    python计算md5值
    python---连接MySQL第五页
    python---连接MySQL第四页
    梯度下降(Gradient Descent)小结
    矩阵的线性代数意义
    矩阵的意义
    偏导数与全导数的关系 以及 偏微分与全微分的关系
    mysql-blog
    python及numpy,pandas易混淆的点
  • 原文地址:https://www.cnblogs.com/studyCenter/p/7354135.html
Copyright © 2011-2022 走看看