2011-06-12 71 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(); 
    } 
}