Java 找不到符号类IOException

Java 找不到符号类IOException,java,exception,Java,Exception,每当我编译上面的代码时,我都会收到一条消息,说找不到符号类IOE异常 有人能告诉我怎么修吗 谢谢你所需要的 public void populateNotesFromFile() { try{ BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED)); String fileNotes = reader.readLine(); while

每当我编译上面的代码时,我都会收到一条消息,说找不到符号类IOE异常

有人能告诉我怎么修吗

谢谢你所需要的

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");
    }       
}
在文件的顶部


另外,FileNotFoundException需要在IOException之上,因为它是IOException的一个子类。

您需要导入文件顶部的java.io.*包,或者将异常完全限定为java.io。IOException是java.io包中的一个类,因此为了使用它,您应该在代码中添加
import
声明<代码>导入java.io.*(位于java文件的最顶端,在包名和类声明之间)


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

切换FileNotFoundException和IOException的顺序您可能缺少对
IOException
类的
导入
引用。它位于
java.io
包中

我能建议你稍微改变一下方法吗?始终关闭finally块中的流:

import java.io;

现在它说,在导入所有io包后,FileNotFoundException e已经被激活FileNotFoundException需要在IOException之上,因为它是IOException的一个子类上周,我没有意识到顺序很重要。彼得大师解释得很好。是的,这是正确的:D对不起,我没有看到答案的第二部分,你一定是编辑了你的答案?!一旦你掌握了进口,进口就有点乏味了。因此,最好将您的IDE(如果它支持的话)配置为自动插入导入声明,以避免类似的问题。同样,当像IOException这样的类名加下划线(显然是缺少导入)时,我会将光标放在带下划线的单词中,然后按Ctrl+1以获取解决错误的提示。如果未导入的类是Date,则可以在java.sql.Date和java.util.Date之间进行选择,等等。。。Ctrl+1是一个非常有用的Eclipse快捷方式。@编辑答案是回答其他问题的正确方法。事实上,你编辑你的问题来包含第二部分会比评论更好——只要它是直接相关的。我还没有这些更专业的IDE的好处,我仍然在学习蓝色J:D谢谢所有的帮助
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");
    }
}