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;
        }
    }
    
  • 相关阅读:
    python os.walk()和os.path.walk()
    python学习之random模块
    java性能优化文章
    mysql和workbench的安装、创建实例、用户
    32位、64位与Java开发研究分析
    Web缓存
    深入分析 Java 中的中文编码问题
    eclipse+tomcat(eclipse自带插件) JSP修改后不生效问题
    今天给2010买的三星R428升级一下固态硬盘
    href="javascript:xxx(this);"和onclick="javascript:xxx(this);"的区别
  • 原文地址:https://www.cnblogs.com/judylucky/p/7047975.html
Copyright © 2011-2022 走看看