我的应用程序的目录结构如下:-
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 类时没有任何成功。请帮忙...
最佳答案
您的属性文件位于类路径中。 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();
}
}
关于java - 无法从 JSF 读取 Java 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6319689/