【发布时间】:2013-11-21 05:22:58
【问题描述】:
我有一个名为“word.txt”的文件。
它与我的java 文件位于同一目录中。
但是当我尝试在下面的代码中访问它时,这个 file not found 会发生错误:
Exception in thread "main" java.io.FileNotFoundException: word.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 Hangman1.main(Hangman1.java:6)
这是我的代码:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
【问题讨论】:
-
指定文件的完整路径
-
要么使用已经说过的完整路径,要么在你的程序中创建一个输出文件来查看它的存储位置,然后将你的输入文件移动到那个位置
-
运行程序时,“word.txt”文件需要和编译器生成的.class文件在同一目录下,而不是.java文件。在像 Netbeans 这样的 IDE 中,将文件放在项目文件夹中,而不是源文件夹中
标签: java file-io filenotfoundexception