我正在尝试获取一个程序来读取文本文件,但即使我在项目目录中设置了potato.txt文件,它也会抛出FileNotFoundException。
import java.io.File;
import java.util.Scanner;
public static void main(String[] args) {
String potato = "potato.txt"; // assume this line can't be changed at all
Scanner scan = new Scanner(new File(potato)); // throws FileNotFoundException
while (scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println(line);
}
}
}
答案 0 :(得分:0)
尝试使用完整的Path,如下所示:
File file = new File("C:/temp/potato.txt"); //This is just an example path, look up your files path and put it here.
Scanner scan = new Scanner(file);
另外,完成后(在while
- 循环之后)不要忘记关闭扫描仪:
scan.close();