无法从JSF读取Java文件

时间:2011-06-12 02:07:12

标签: 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课程时,没有任何成功。请帮忙......

2 个答案:

答案 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();
    }
}

答案 1 :(得分:0)

我不知道这是不是你的问题,但你应该尝试使用斜杠而不是句点,因为它们被存储为文件系统中的实际文件夹。