java讀取資源文件的方式

java裏面常用的 讀取資源文件方式大致兩種,但是當我們應用到web應用程序中的時候,第一種方式如果使用相對路徑則可能會讀取不到資源文件
第一種方式:
Properties dbProps = new Properties();
InputStream fileinputstreams = new FileInputStream("src/spf.properties");
dbProps.load(fileinputstream);
第二種方式:
Properties dbProps = new Properties();
ClassLoader cl  =  Thread.currentThread().getContextClassLoader();
InputStream fileinputstream=cl.getResourceAsStream("spf.properties");
dbProps.load(fileinputstream);
發佈了35 篇原創文章 · 獲贊 5 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章