2011-10-21 33 views
0

我的java類位於文件夾/webserver/tomcat-instance/webapps/shop/WEB-INF/classes/com/mobile/commons中,但實際屬性文件位於 /webserver/tomcat-instance/webapps/shop/WEB-INF/classes/resources/webserver/tomcat-instance/webapps/shop/WEB-INF/classes/resources目錄中。現在在我的java文件中,我把相對路徑設置爲/WEB-INF/classes/resources/prop.properties來讀取預期的文件,但每次我得到FileNotFound異常。無法通過使用相對路徑讀取屬性文件的內容

請幫我解決這個問題。我不應該使用絕對路徑,因此我應該將其用作相對路徑。

感謝, Sourav

+0

有關如何嘗試讀取文件的更多信息,或者您想讓我們猜測嗎? – Thomas

回答

1

嘗試與閱讀它

this.getClass().getClassLoader().getResourceAsStream("resources/prop.properties"); 

更新:

URL resource = classLoader.getResource("resources/prop.properties"); 
File file = new File(resource.toURI()); 
FileInputStream input = new FileInputStream(file); 
+0

是的!提到的方法爲我工作..感謝很多。我還有一個問題,即如何通過使用FileInputStream而不是InputStream來讀取同一個屬性文件? –

+0

查看最新版本

1

,最好使用

Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/prop.properties") 
+0

如果您正在加載類,最好是獲取上下文ClassLoader。但我不確定在閱讀資源流的情況下它會如何改變。 – Thor84no

相關問題