1. 程式人生 > 實用技巧 >載入properties檔案六種方法

載入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"));
    }
https://www.cnblogs.com/u1s1/p/12425562.html