javac错误:扫描程序:FileNotFoundException

问题描述 投票:0回答:1

我现在正在编写一个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());将打印出正确的并存在的文件路径)。

java java.util.scanner javac java-io
1个回答
0
投票

似乎您没有导入类FileNotFoundException

在文件顶部添加import java.io.FileNotFoundException应该可以解决此问题。


0
投票

您需要在课程开始时添加正确的导入:

import java.io.FileNotFoundException;

public class Myclass {...
© www.soinside.com 2019 - 2024. All rights reserved.