我須要在配置文件中設置一些參數,而後在代碼中獲取數據使用。個人配置文件放在了src/main/resources文件夾下面了。java
主要是經過當前類加載器,經過加載resources資源文件爲流,而後使用Properties類的load方法加載資源文件流便可,讀取資源文件中的參數。code
Properties props = new Properties(); ClassLoader classLoader = getClass().getClassLoader(); InputStream stream = classLoader.getResourceAsStream("views.properties"); try { props.load(stream); props.getProperty("mykey", "myvalue");// 該方法便可使用數據 } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } }
**Note:**注意關閉流操做。資源
參考: Java – Read a file from resources folder Reading Properties fileget