【发布时间】:2020-02-26 19:38:31
【问题描述】:
我现在正在编写一个 java 类并想读取一个 txt 文件,如下所示:
public class Myclass {
...
public static void main(String[] args) {
try{
File file = new File(args[0]);
Scanner scanner = new Scanner(file);
int [] array = new int [1000];
int i = 0;
while(scanner.hasNextInt())array[i++] = scanner.nextInt();}
catch (FileNotFoundException exp) {
exp.printStackTrace();}
}
...
}
例如,像java Myclass input.txt 一样使用它。但是,当我在 Linux 中使用 javac 编译它时,会抛出错误:
error: cannot find symbol
catch (FileNotFoundException exp) {
^
symbol: class FileNotFoundException
location: class Myclass
这很奇怪,因为输入文件的名称甚至还没有传入。我试过File file = new File('input.txt');,它也会抛出这个错误,所以我不知道出了什么问题(System.out.println(new File('input.txt').getAbsolutePath());将打印出正确的和存在路径)。
【问题讨论】:
-
尝试将其从
FileNotFoundException更改为通用Exception看看是否仍然发生 -
还是,编译不通过。
标签: java java.util.scanner javac java-io