加載項目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類靜態方法加載
* 2020.11.3 最新經驗,該方法可能在web項目,或者mvn -exec:運行時讀取不到配置文件,需要用上面的方法 */ 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")); }