【问题标题】:FileNotFoundException not being thrownFileNotFoundException 没有被抛出
【发布时间】:2014-10-31 06:40:30
【问题描述】:

我正在编写一段代码,它会返回一个扫描器以获取用户输入的文件名。代码如下:

public static Scanner getInputScanner(Scanner console) {
    System.out.print("Enter input file: ");
    String fileName = "";
    try {
        fileName = console.nextLine();
        File f = new File(fileName);
    } catch (FileNotFoundException e) {      
        while (!(new File(fileName)).exists()) {
            System.out.println(fileName + " (No such file or directory)");
            System.out.print("Enter input file: ");
            fileName = console.nextLine();
        }
    }
    File f = new File(fileName);
    return new Scanner(f);
}

我收到两个错误:

Compression.java:49: error: exception FileNotFoundException is never thrown in body of corresponding try statement
    } catch (FileNotFoundException e) {      
      ^
Compression.java:57: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    return new Scanner(f);

我不知道为什么 try 块没有抛出异常,因为用户可能输入了无效的文件名。

感谢您的帮助。

编辑:将 FileNotFoundException 更改为 NullPointerException 并解决了第一个问题。但是,现在我收到一个错误,即我的 return 语句正在引发未报告的 FileNotFoundException。但是除非文件有效,否则这段代码不会执行,对吧? Java对此是否视而不见,并且无论如何都需要我捕获异常?

【问题讨论】:

    标签: java try-catch filenotfoundexception


    【解决方案1】:

    FileNotFoundException 既不是由Scanner#nextLine() 抛出的,也不是通过创建一个新的File 对象(File#new(String))。这两个函数都不做与文件 I/O 相关的事情。

    • Scanner.nextLine() 在一个已经存在的输入源上运行
    • File#new() 仅创建一个新的 File 对象,该对象将(文件名)指向(可能存在的)实际文件。

    相比之下,new Scanner object 的创建涉及创建一个新的InputStream,因此它实际上通过打开它来触及提供的文件。

    来自java.util.Scanner

    public Scanner(File source) throws FileNotFoundException {
        this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
    }
    

    【讨论】:

      【解决方案2】:

      The documentation 明确指出File(String pathname) 构造函数只能抛出NullPointerException 而不能抛出FileNotFoundException

      如果要查看文件名是否有效,请使用f.exists()

      【讨论】:

        【解决方案3】:
         return new Scanner(f);
        

        找不到文件时抛出错误,它不能返回 scanner(f) 。所以应该包裹在try-catch 块中。

        或者你需要让getInputScanner 抛出FileNotFoundException

        【讨论】:

          【解决方案4】:
          File f = new File(fileName);
          

          如果文件不存在,not 是否会抛出异常。 File 对象实际上只是一个文件名;它不引用实际文件。如果文件不存在,尝试使用时会出现异常。

          new Scanner(f) 是抛出FileNotFoundException 的部分。

          【讨论】:

            【解决方案5】:

            在构造Scanner 之前,您始终可以调用File.exists(),如果使用无限循环,您可以简化逻辑并消除这些错误。类似的,

            public static Scanner getInputScanner(Scanner console) {
                while (true) {
                    System.out.print("Enter input file: ");
                    String fileName = console.nextLine();
                    File f = new File(fileName);
                    if (!f.exists()) {
                        System.out.println(fileName + " (No such file or directory)");
                        continue;
                    }
                    try {
                        return new Scanner(f);
                    } catch (FileNotFoundException e) {
                        // This shouldn't happen.
                        e.printStackTrace();
                        System.out.println(fileName + " (No such file or directory)");
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-06-19
              • 2020-07-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-11-09
              • 2018-07-31
              相关资源
              最近更新 更多