2013-03-06 43 views
0

方法如何使用File對象在maven webapp中加載屬性文件?

getResourceAsStream 

工作正常,但我需要檢查以及文件被修改或沒有。這是使用File對象的原因。我無法實現這個目標。

File file = new File("goods.props") 

在JUnit環境中工作正常,但在webapp中,我得到一個FileNotFoundException。

其實我插入代碼加載道具文件的下一行:

File file = new File(".") 
Sysout(file.getAbsolutePath()) 

在JUnit環境控制檯輸出爲:

C:\Users\developer\workspace\Projects\WebApp\. 

這是正確的

但是,當我開始的webapp,輸出是:

C:\Programm\eclipseJEE 

這是一個eclipse的文件夾。

回答

0

你可以做這樣的事情:

File f = new File(getClass().getClassLoader().getResource("goods.props").toURI()).getAbsolutePath(); 
相關問題