线程“main"中的异常java.io.FileNotFoundException:错误 [英] Exception in thread "main" java.io.FileNotFoundException: Error
本文介绍了线程“main"中的异常java.io.FileNotFoundException:错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Eclipse 来编译和运行我的 Java 代码.
I am using Eclipse to compile and run my java codes.
这是我遇到的错误.
Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at helloworld.main(helloworld.java:9)
这是我的代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class helloworld {
public static void main(String[] args) throws IOException {
Scanner KB = new Scanner(new File("file.txt"));
while (KB.hasNext()) {
String line = KB.nextLine();
System.out.println(line);
}
}
}
文件.txt
我在项目的同一文件夹中创建了 file.txt.
File.txt
I have created file.txt in same folder in my project.
推荐答案
你的文件应该直接在项目文件夹下,而不是在任何其他子文件夹内.
Your file should directly be under the project folder, and not inside any other sub-folder.
所以,如果你的项目文件夹是MyProject
,它的文件夹结构(虽然不完整)应该是这样的:-
So, if your project folder is MyProject
, it's folder structure(not complete though) should be like: -
MyProject +- src +
| |
| +-- Your source file
+- file.txt
它不应该在 src
文件夹下.
It should not be under src
folder.
或者,您可以给出以下相对于项目文件夹的路径来搜索src文件夹
中的文件:-
Or, you can give the following path relative to the project folder to search for file in the src folder
: -
new File("src/file.txt");
这篇关于线程“main"中的异常java.io.FileNotFoundException:错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文