2011-06-12 42 views
0

閱讀Java文件我的應用程序的目錄結構如下: -無法從JSF

My App 
++++++ src 
++++++++com 
++++++++++readProp.java 
++++++++resource 
++++++++++message.properties 

我試圖讀取該文件,如下所示: -

public Static final string FilePath="resource.message.properties" 

下面的代碼讀取文件。我嘗試使用以下兩種方法,但都沒用......

File accountPropertiesFile = new File(FacesContext.getCurrentInstance() 
.getExternalContext().getRequestContextPath() 
+ FilePath); 

properties.load(externalContext.getResourceAsStream(FilePath)); 

但通過Bean類讀書時,沒有任何一代產量sucess。請幫助...

回答

0

我不知道這是否是您的問題,但您應該嘗試使用斜線而非句點,因爲它們作爲實際文件夾存儲在文件系統中。

1

您的屬性文件位於類路徑中。 java.io.File只理解本地磁盤文件系統結構。這是行不通的。您需要通過類加載器從類路徑中直接獲取它。

這裏有一個開球例如:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
InputStream input = classLoader.getResourceAsStream("/resources/messages.properties"); 

if (input != null) { 
    Properties properties = new Properties(); 
    try { 
     properties.load(input); 
    } finally { 
     input.close(); 
    } 
}