【问题标题】:Java error: Default constructor cannot handle exception type FileNotFound ExceptionJava 错误:默认构造函数无法处理异常类型 FileNotFound Exception
【发布时间】:2012-02-20 00:28:21
【问题描述】:

我正在尝试从文件中读取输入以将其放入 Java 小程序以显示为 Pac-man 级别,但我需要使用类似于 getLine() 的东西......所以我搜索了类似的东西,这是我找到的代码:

File inFile = new File("textfile.txt");
FileInputStream fstream = new FileInputStream(inFile);//ERROR
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

我标记为“ERROR”的行给了我一个错误,上面写着“默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。必须定义一个显式构造函数。”

我已经搜索过此错误消息,但我发现的所有内容似乎都与我的情况无关。

【问题讨论】:

标签: java file-io constructor compiler-errors


【解决方案1】:

在您的子类中声明一个显式构造函数,该构造函数会抛出 FileNotFoundException

public MySubClass() throws FileNotFoundException {
} 

或者用try-catch 块包围基类中的代码,而不是抛出FileNotFoundException 异常:

public MyBaseClass()  {
    FileInputStream fstream = null;
    try {
        File inFile = new File("textfile.txt");
        fstream = new FileInputStream(inFile);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        // Do something with the stream
    } catch (FileNotFoundException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            // If you don't need the stream open after the constructor
            // else, remove that block but don't forget to close the 
            // stream after you are done with it
            fstream.close();
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }  
} 

无关,但由于您正在编写 Java 小程序,请记住您需要 sign it 才能执行 IO 操作。

【讨论】:

    【解决方案2】:

    您需要使用 try 和 catch 包围您的代码,如下所示:

    try {
        File inFile = new File("textfile.txt");
        FileInputStream fstream = new FileInputStream(inFile);//ERROR
    } catch (FileNotFoundException fe){
        fe.printStackTrace();
    }
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    

    【讨论】:

    • Adel,如果您这样编写代码,fstreamDataInputStream 构造函数不可见。您需要重构您的代码,并在 try 块之外引用一个空值 fstream,就像我在示例中所做的那样。此外,由于您没有使用 try / catch 语句包围整个块,因此在声明 inbr 变量之前检查 fstream != null 并不是一个坏主意。
    • @AnthonyAccioly 我没有提供完整的解决方案,我只是给了你一个线索,try 和 catch 应该包含所有可能产生上述错误的行。可悲的是,我没有检查哪些行会这样做。您可以在方便时使用它:)
    【解决方案3】:

    这是猜测,因为我们没有完整的代码。

    来自 Javadoc:

    public FileInputStream(File file) throws FileNotFoundException
    

    这意味着当您像您一样执行新的FileInputStream() 时,它可以返回FileNotFoundException。这是一个已检查的异常,您需要重新抛出(即在执行新操作的方法中添加“抛出 FileNotFoundException”)或捕获(参见其他 try/catch 响应)。

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 2014-07-07
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多