<分区>
我正在尝试从我的 war 存档中读取一个文本文件,并在运行时在 facelets 页面中显示内容。我的文件夹结构如下
+war archive > +resources > +email > +file.txt
我尝试使用以下代码读取 resources/email/file.txt 文件夹中的文件
File file = new File("/resources/email/file.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer buffer = new StringBuffer();
if (reader != null) {
String line = reader.readLine();
while (line != null) {
buffer.append(line);
line = reader.readLine();
// other lines of code
但是问题是,当我运行带有上述代码的方法时,将抛出 FileNotFoundException
。我也试过用下面这行代码来获取文件,但是没有成功
File file = new File(FacesContext.getCurrentInstance()
.getExternalContext().getRequestContextPath() + "/resources/email/file.txt");
我仍然得到 FileNotFoundException
。这是怎么引起的,我该如何解决?