FileNotFound如果放在src文件夹Java Eclipse之外

时间:2022-06-13 11:00:23

I am using Eclipse IDE to develop a java program that reads a text file. I am using FileReader class to read a text file in Java. It works fine, if I place the text file inside the src folder of the project, but when I place it in some other location, I get a FileNotFoundException. I have no idea what must be wrong with it. Some help will be much appreciated.

我正在使用Eclipse IDE开发一个读取文本文件的java程序。我正在使用FileReader类来读取Java中的文本文件。它工作正常,如果我将文本文件放在项目的src文件夹中,但当我把它放在其他位置时,我得到一个FileNotFoundException。我不知道它有什么问题。一些帮助将不胜感激。

2 个解决方案

#1


-1  

You should create File object like this:

您应该像这样创建File对象:

File f = new File("D://new folder//file.txt");

and take this object to File reader

并将此对象带到文件阅读器

#2


0  

File reader is trying to read the file from classpath by default, that's why it doesn't fail when you place the file inside the classpath (src) path. When you place the file outside of the src folder, you have to give full path to read the file. Example : File f=new File("\home\user\xxx\file.txt");

文件阅读器默认尝试从类路径读取文件,这就是当您将文件放在类路径(src)路径中时它不会失败的原因。将文件放在src文件夹之外时,必须提供完整的路径来读取文件。示例:文件f =新文件(“\ home \ user \ xxx \ file.txt”);

#1


-1  

You should create File object like this:

您应该像这样创建File对象:

File f = new File("D://new folder//file.txt");

and take this object to File reader

并将此对象带到文件阅读器

#2


0  

File reader is trying to read the file from classpath by default, that's why it doesn't fail when you place the file inside the classpath (src) path. When you place the file outside of the src folder, you have to give full path to read the file. Example : File f=new File("\home\user\xxx\file.txt");

文件阅读器默认尝试从类路径读取文件,这就是当您将文件放在类路径(src)路径中时它不会失败的原因。将文件放在src文件夹之外时,必须提供完整的路径来读取文件。示例:文件f =新文件(“\ home \ user \ xxx \ file.txt”);