zoukankan      html  css  js  c++  java
  • 运行jar包读取外部配置文件

    测试路径:

    	public static void main(String[] args) {
    		String path1 = System.getProperty("user.home");//当前登录用户目录
    		String path2 = System.getProperty("user.dir");//jar包所在目录名
    		System.out.println(path1+"=="+path2);
    	}
    

     使用列子:

     public static void main(String[] args) {
            try {
                PropContainer pro = PropContainer.getInstance(System
                        .getProperty("user.dir")
                        + File.separator
                        + "config.properties");
                InitExcelData.init(pro.getProperty("excelPath"));
    
                String sourcePath = pro.getProperty("sourcePath");
                String tempPath = pro.getProperty("tempPath");
                String destPath = pro.getProperty("destPath");
                String errorPath = pro.getProperty("errorPath");
                String bakPath = pro.getProperty("bakPath");
                String isBak = pro.getProperty("isBak");
           } catch (Exception e) {
                log.info(e.getMessage());
                e.printStackTrace();
            }
    
        }
    
    public class PropContainer {
        private static final Logger log = Logger.getLogger(PropContainer.class);
    
        private Properties properties;
    
        private static String charset = "UTF-8";
    
        private static Map<String, PropContainer> map = new HashMap<String, PropContainer>();
    
        private PropContainer(String propertiesWay) {
            initConfig(propertiesWay);
        }
    
        public static PropContainer getInstance(String propertiesWay) {
            if (map.containsKey(propertiesWay)) {
                return map.get(propertiesWay);
            }
            PropContainer p = new PropContainer(propertiesWay);
            map.put(propertiesWay, p);
            return p;
        }
    
        private void initConfig(String propertiesWay) {
            properties = new Properties();
            try (InputStreamReader in = new InputStreamReader(new FileInputStream(
                    propertiesWay), charset)) {
                properties.load(in);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    
        public String getProperty(String key, String defaultValue) {
            if (properties == null) {
                return defaultValue;
            }
            return properties.getProperty(key, defaultValue);
        }
        
        public String getProperty(String key) throws RuntimeException{
            String tmp = properties.getProperty(key);
            if(StringUtils.isBlank(tmp)){
                throw new RuntimeException("属性为空!"+key);
            }
            return tmp;
        }
    
        public int getInt(String key, int defaultValue) {
            String prop = getProperty(key, "");
            if ("".equals(prop) || !NumberUtils.isDigits(prop)) {
                return defaultValue;
            }
            return Integer.parseInt(prop);
        }
    
        public boolean containsKey(String key) {
            if (properties != null && properties.containsKey(key)) {
                return true;
            }
            return false;
        }
    }
    
  • 相关阅读:
    JavaScript对象继承的实现
    Redis资料
    Difference between LINQ to SQL and the Entity Framework
    闭包,懂不懂由你,反正我是懂了
    Castle资料
    csu 1242 碱基配对
    csu 1242 碱基配对——一个错误的解答
    [转载]zoj 分类
    计算素数
    魔方阵
  • 原文地址:https://www.cnblogs.com/judylucky/p/7047975.html
Copyright © 2011-2022 走看看