获取FileNotFoundException [英] Getting FileNotFoundException

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

问题描述

我有一个Web项目,正在使用以下代码:

I have a Web Project where I am using the following code:

try {
    br1 = new BufferedReader(new FileReader("queryWords.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

现在,它引发了以下异常:

Now for this, it is throwing me the following exception:

java.io.FileNotFoundException: queryWords.txt (The system cannot find the file specified)

(我正在将"queryWords.txt"文件存储在项目文件夹本身中)

(I am storing 'queryWords.txt' file in the Project Folder itself)

当我将代码更改为以下内容时:

When I change my code to the following:

br1 = new BufferedReader(new FileReader("C://Users//Project//Demo//queryWords.txt"));

我给出了完整的绝对路径,它可以完美运行,但是当我将项目更改为平面"Java Project"时,初始代码可以正常运行,并且没有任何异常.

where I have given the full absolute path, it runs perfectly but when I change my project to a plane 'Java Project', the initial code works perfectly fine and I am not getting any exception.

那么为什么发生这种意外行为?当我的项目只是"Java项目"时,我不必提供完整的路径,但是当我有动态Web项目"时,如果我不提供文件的完整绝对路径,则会收到错误消息.

So why this unexpected behavior is happening? When my project is just 'Java Project', I don't have to provide the full path but when I have the 'Dynamic Web Project', I am getting the error if I don't give the full absolute path of the file.

注意:我正在使用Eclipse IDE.

Note:I am using Eclipse IDE.

推荐答案

当我的项目只是'Java Project'时,我不必提供完整的路径"

"When my project is just 'Java Project' I don't have to provide the full path"

您的IDE正在从工作目录(通常是项目根目录)中读取文件.

Your IDE is reading the file from working directory which is normally the project root.

当我拥有动态Web项目"时,如果不提供文件的完整绝对路径,则会收到错误消息"

"when I have the 'Dynamic Web Project', I am getting the error if I don't give the full absolute path of the file"

Web应用程序打包到一个.war(.jar)中.构建应用程序后,src中的文件将包含在类路径中,应照此读取.对于您而言,文件从未包含在jar中,因为它不是src中的资源.即使从文件系统读取文件可能在您的系统上也可以使用,但使用该应用程序的其他任何人都无法读取该文件.

Web application are packaged in to a .war(.jar). Once the application is built, the files in the src are included into the class path, and should be read as such. In your case, your file was never included into the jar, as it's not a resource within the src. Even though reading the file from the file system might work on your system, no one else using the application will be able to read it.

相反,您应该在src中拥有您的资源,例如resources软件包.这取决于构建的结构,应提供的路径,但是我认为使用的是普通的maven项目结构(文件位于src/main/resources/myfile.txt中)和常规的Java项目结构(src/resources) /myfile.txt),您可以安全地使用以下

Instead you should have your resources in the src, say a resources package. It depends on the structure of your build, what path you should provide, but I think with a normal maven project structure (with the file in the src/main/resources/myfile.txt) and a regular java project structure (src/resources/myfile.txt) you are safe to use the following

InputStream is = getClass().getResourceAsStream("/resources/myfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

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

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