zoukankan      html  css  js  c++  java
  • 加载properties文件六种方法

    加载项目properties文件的六种方法,其中四种都是通过Properties类加载inputStream读取,后两种通过ResourcesBundle类和其子类来加载

    复制代码
        /**
         * 通过inputStream加载配置文件到properties对象
         */
    
        private void getPropertiesByInputStream_One() throws IOException {
            //全路径
            String path = "/Users/grahamliu/idea-workspace/AppiumAIDemo/src/main/resources/appium.properties";
            Properties properties = new Properties();
    
            InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
            properties.load(inputStream);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(properties.getProperty("appiumUrl"));
        }
    
        /**
         * 通过class类getResourceAsStream方法加载配置文件流
         */
        private void getPropertiesByInputStream_Two() throws IOException {
            //路径/开头,表示从classpath下取路径
    //        String path = "/appium.properties";
            //路径不为/开头,从当前类所在包下取
            String path = "appiumRelative.properties";
    
            Properties properties = new Properties();
            InputStream inputStream = this.getClass().getResourceAsStream(path);
            properties.load(inputStream);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(properties.getProperty("appiumUrl"));
        }
    
        /**
         *通过class的类加载器getClassLoader加载配置
         */
        private void getPropertiesByInputStream_Three() throws IOException{
            //getClassLoader默认加载路径就是classpath,规定不需要用/开头文件路径
            String path = "appium.properties";
    
            Properties properties= new Properties();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            properties.load(inputStream);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(properties.getProperty("appiumUrl"));
        }
    
        /**
         * 通过ClassLoader类静态方法加载
         */
        private void getPropertiesByInputStream_Four() throws IOException{
            //ClassLoader默认加载路径就是classpath,规定不需要用/开头文件路径
            String path = "appium.properties";
    
            Properties properties = new Properties();
            InputStream inputStream = ClassLoader.getSystemResourceAsStream(path);
            properties.load(inputStream);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(properties.getProperty("appiumUrl"));
        }
    
        /**
         * 通过ResourceBundle的构造方法getBundle
         */
        private void getPropertiesByResourceBundle_Five(){
            //这个getBundle()方法的参数相对同目录路径,并去掉.properties后缀,否则将抛异常
            String path = "appium";
            ResourceBundle resourceBundle  = ResourceBundle.getBundle(path);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(resourceBundle.getString("appiumUrl"));
        }
    
        /**
         * 通过ResourceBundle子类PropertyResourceBundle加载inputStream
         */
        private void getPropertiesByResourceBundle_Six() throws IOException{
            String path = "/Users/grahamliu/idea-workspace/AppiumAIDemo/src/main/resources/appium.properties";
            InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
            ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
    
            System.out.println(this.getClass().getName()+"+"+Thread.currentThread().getStackTrace()[1].getMethodName());
    
            System.out.println(resourceBundle.getString("appiumUrl"));
        }
    复制代码
  • 相关阅读:
    电子书下载:Beginning ASP.NET 2.0 AJAX
    电子书下载:C# 4.0 How To
    电子书下载:Pragmatic Unit Testing in C# with NUnit
    7 个最好的.Net开源CMS系统
    电子书下载:Professional ASP.NET 2.0 Server Control and Component Development
    电子书下载:Beginning ASP.NET 2.0 Databases From Novice to Professional
    电子书下载:Professional .NET 2.0 Generics
    蛙蛙推荐:[算法练习]最长不完全匹配子串频率计算
    蛙蛙推荐:F#实现并行排序算法
    蛙蛙推荐:蛙蛙教你发明一种新语言之二代码生成
  • 原文地址:https://www.cnblogs.com/globalcoding/p/13359211.html
Copyright © 2011-2022 走看看