zoukankan      html  css  js  c++  java
  • 读取 classes下的配置文件

    调用:

    Configure.getValue("discount.strategy.class");
    

      

    配置类:

    package com.util;
    
    import com.sun.javafx.fxml.PropertyNotFoundException;
    
    import java.io.InputStream;
    import java.util.Properties;
    
    public class Configure {
    
        private static Properties config;
    
        static {
            System.out.println("初始化加载配置!");
    
            String filePath = "application.properties";
    
            config = new Properties();
            try {
                ClassLoader CL = Configure.class.getClassLoader();
                InputStream in;
                if (CL != null) {
                    in = CL.getResourceAsStream(filePath);
                } else {
                    in = ClassLoader.getSystemResourceAsStream(filePath);
                }
                config.load(in);
                in.close();
            } catch (Exception e) {
                throw new PropertyNotFoundException("服务器配置信息读取错误:" + e.getMessage());
            }
        }
    
        public static String getValue(String key) {
            if (config.containsKey(key)) {
                String value = config.getProperty(key);
                return value;
            } else {
                return "";
            }
        }
    
        public static int getValueInt(String key) {
            String value = getValue(key);
            int valueInt = 0;
            try {
                valueInt = Integer.parseInt(value);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return valueInt;
            }
            return valueInt;
        }
    }
    

      

  • 相关阅读:
    2016huasacm暑假集训训练四 递推_A
    2016huasacm暑假集训训练三 G
    2016huasacm暑假集训训练三 F
    今年暑假不AC
    Who's in the Middle
    The Suspects
    食物链
    抓牛问题
    士兵队列训练问题
    H
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/9279481.html
Copyright © 2011-2022 走看看