[英]FileNotFoundException when ussing FileReader to find a resource in a java ee6 dynamic project
I am getting the all well known FileNotFoundException when i try to call a .txt file that holds some text. 当我尝试调用包含一些文本的.txt文件时,我得到了众所周知的FileNotFoundException。 I used various path convinations, but i dont get the right one.
我使用了各种路径说服力,但是我没有得到正确的说服力。 Here is how i call it:
这是我的称呼:
private String generateActivationLinkTemplate() {
String htmlText = "";
try {
Scanner scanner = new Scanner(new FileReader(
"/web/emailActivationTemplate.txt"));
while (scanner.hasNextLine()) {
htmlText += scanner.nextLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return htmlText;
}
The full path to the file looks like this: 文件的完整路径如下所示:
C:\jee6workspace\BBS\WebContent\web\emailActivationTemplate.txt
How should i tell my program to find this file in the most flexible way? 我应该如何告诉我的程序以最灵活的方式找到该文件?
One way - access to file through servlet context, fe i'm use in my Wicket project 一种方法-通过Servlet上下文访问文件,例如在我的Wicket项目中使用
final ServletContext ctx = ((WebApplication) getApplication()).getServletContext();
final File reportFile = new File(ctx.getRealPath("/reports/pivotTable.jasper"));
in your case 在你的情况下
Scanner scanner = new Scanner(new FileReader(
new File(ctx.getRealPath("/web/emailActivationTemplate.txt"))));
so you only need to get your servlet context in proper place Other way - try to access to file through classpath. 因此,您只需要将servlet上下文放在适当的位置即可。其他方式-尝试通过类路径访问文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.