【发布时间】:2017-01-18 12:10:09
【问题描述】:
我正在尝试让程序读取文本文件,但它会抛出 FileNotFoundException,即使我在项目目录中设置了 potato.txt 文件。
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);
}
}
}
【问题讨论】:
-
你的文件到底在哪里?
-
尝试使用绝对路径。
-
请发布项目结构
标签: java java.util.scanner java-io