JAVA WEB 獲取properties檔案,存放路徑 及 獲取路徑
阿新 • • 發佈:2019-02-10
首先明白,ClassPath : 指的是:WEB-INF\classes這個目錄下,通常把檔案放在src資料夾下,或者新建檔案並且新增到build path即可。IDE在同步檔案到tomcat時會自動將檔案放在classes下。
獲取properties的程式碼:
public Properties loadProperties() { Properties properties = new Properties(); InputStream stream = this.getClass().getClassLoader() .getResourceAsStream("/xxx.properties"); try { properties.load(stream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return properties; }
請注意:路徑
/handlerFactory.properties
有/表示從classpath根目錄下查詢,無/表示從當前包下查詢。
這次被坑的最重要一點:
Properties檔名首字母不能大寫,
Properties檔名首字母不能大寫,
Properties檔名首字母不能大寫,
大寫了你一輩子找不到檔案。!
重要的事情說三遍!