【问题标题】:cannot find symbol class IOException [duplicate]找不到符号类 IOException [重复]
【发布时间】:2010-10-26 16:55:23
【问题描述】:
public void populateNotesFromFile()
{
    try{
        BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
        String fileNotes = reader.readLine();

        while(fileNotes != null){
            notes.add(fileNotes);
            fileNotes = reader.readLine();
        }
        reader.close();
    }
    catch (IOException e){
        System.err.println("The desired file " + DEFAULT_NOTES_SAVED + " has problems being read from");
    }
    catch (FileNotFoundException e){
        System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
    }

    //make sure we have one note
    if (notes.size() == 0){
        notes.add("There are no notes stored in your note book");
    }       
}

每当我编译上述代码时,我都会收到一条消息,说找不到符号类 IOException e

谁能告诉我如何解决它:d

谢谢

【问题讨论】:

  • 现在它说在导入所有 io 包后 FileNotFoundException e 已经被捕获
  • FileNotFoundException 需要高于 IOException,因为它是 IOException 的子类。

标签: java exception


【解决方案1】:

您可能缺少对IOException classimport 引用。我位于java.io 包中。

我可以建议对您的方法进行一些更改吗?始终在 finally 块中关闭流:

public void populateNotesFromFile() {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED));
        String fileNotes = reader.readLine();
        while (fileNotes != null) {
            notes.add(fileNotes);
            fileNotes = reader.readLine();
        }
    } catch (FileNotFoundException e) {
        System.err.println("Unable to open " + DEFAULT_NOTES_SAVED);
    } catch (IOException e) {
        System.err.println("The desired file " + DEFAULT_NOTES_SAVED
                + " has problems being read from");
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // make sure we have one note
    if (notes.size() == 0) {
        notes.add("There are no notes stored in your note book");
    }
}

【讨论】:

    【解决方案2】:

    你需要

    import java.io;
    

    在文件的顶部。

    此外,FileNotFoundException 必须高于 IOException,因为它是 IOException 的子类。

    【讨论】:

      【解决方案3】:

      切换FileNotFoundException和IOException的顺序

      【讨论】:

      • 是的,我这样做了,它奏效了:D 你能告诉我为什么会这样吗,因为这是我目前正在做的所有练习,我一周后要考试,但我没有t意识到订单很重要
      • MasterPeter 已经解释得很好了。
      【解决方案4】:

      IOException 是 java.io 包中的一个类,所以为了使用它,你应该在你的代码中添加一个import 声明。 import java.io.*;(在 java 文件的最顶部,在包名和你的类声明之间)

      FileNotFoundException 是一个 IOException。它是 IOException 的一个特化。一旦您捕获了 IOException,程序的流程将永远不会检查更具体的 IOException。只需交换这两个,首先测试更具体的情况(FileNotFound),然后然后处理(捕获)任何其他可能的 IOExceptions。

      【讨论】:

      • 是的,这是正确的:D 抱歉,我最初没有看到您的第二部分答案,您一定已经编辑了答案?!
      • 在您掌握它们之后,导入是有些乏味的工作。所以最好配置你的IDE(如果它支持的话)自动插入导入声明以避免这样的问题。
      • 例如在 Eclipse 中,我不时按 Ctrl+Shift+O 来组织导入。此外,当像 IOException 这样的类名带有下划线(显然是缺少导入)时,我将光标放在带下划线的单词中,然后按 Ctrl+1 以获取解决错误的提示。如果未导入的类是 Date,您可以在 java.sql.Date 和 java.util.Date 等之间进行选择...Ctrl+1 是一个非常有用的 Eclipse 快捷方式。
      • @compsciundergrad 编辑答案是回答其他问题的正确方法。事实上,你编辑你的问题以包含第二部分会比评论更好 - 只要它是直接相关的。
      • 我还没有这些更专业的 IDE 的好处,我还在用 Blue J 学习 :D 感谢大家的帮助
      【解决方案5】:

      您需要在文件顶部导入 java.io.* 包,或者将异常完全限定为 java.io.IOException

      【讨论】:

        猜你喜欢
        • 2010-10-12
        • 2019-05-31
        • 2015-06-07
        • 2020-06-13
        • 2012-09-09
        • 2011-10-29
        • 2015-12-15
        相关资源
        最近更新 更多