无法从JSF读取Java文件 [英] Unable to read Java File from JSF

查看:69
本文介绍了无法从JSF读取Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的目录结构如下:-

The directory structure of my application is as follows:-

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

我正在尝试按以下方式读取文件:-

I am trying to read the file as follows:-

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

这里是读取文件的代码.我尝试使用以下两种技术,但无济于事...

Here the code to read the file. I tried using the following two techniques but to no use...

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

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

但是在遍历Bean类时,没有任何成功.请帮忙...

But none yeild any sucess while reading through the Bean class. please help...

推荐答案

您的属性文件位于类路径中. java.io.File仅了解本地磁盘文件系统结构.这是行不通的.您需要通过类加载器直接从类路径中获取它.

Your properties file is in the classpath. The java.io.File only understands the local disk file system structure. This is not going to work. You need to get it straight from the classpath by the classloader.

这是一个开球示例:

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

这篇关于无法从JSF读取Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆