【问题标题】:error: cannot find symbol: class FileNotFoundException错误:找不到符号:类 FileNotFoundException
【发布时间】:2015-01-26 10:45:22
【问题描述】:

我收到以下错误:

printfile.java:6: error: cannot find symbol

throws FileNotFoundException {
                   ^
  symbol:   class FileNotFoundException
  location: class printfile

以下代码:

import java.io.File;

import java.util.Scanner;

    public class printfile {

        public static void main(String[]args) 

            throws FileNotFoundException {
            Scanner keyboard = new Scanner(System.in);
            System.out.println (" What file are you looking for? ");
            String searchedfile = keyboard.next();
            File file = new File(searchedfile);
            if (file.exists()) {
                System.out.println(" Okay, the file exists... ");
                System.out.print(" Do you want to print the contents of " + file + "?");
                String response = keyboard.next();
                if (response.startsWith("y")) {
                    Scanner filescan = new Scanner(file);
                        while (filescan.hasNext()) {
                        System.out.print(filescan.next());
                        }
                }   
                else {
                    System.out.print(" Okay, Have a good day.");
                }
        }
    }
}

如何解决这个错误?

【问题讨论】:

  • import java.io.FileNotFoundException...

标签: java file-io error-handling runtime-error java.util.scanner


【解决方案1】:

要使用不在程序“范围”内的类(即FileNotFoundException),您必须:

调用它的完全限定名称:

// Note you have to do it for every reference.
public void methodA throws java.io.FileNotFoundException{...} 
public void methodB throws java.io.FileNotFoundException{...} 

从它的包中导入类:

// After import you no longer have to fully qualify.
import java.io.FileNotFoundException;

...

public void methodA throws FileNotFoundException{...} 
public void methodB throws FileNotFoundException{...} 

建议同时查看this 问题,该问题几乎解释了您可能想了解的有关 Java 访问控制修饰符的所有信息。

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2020-05-03
    • 2019-01-13
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多