Java 找不到符号类 IOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/872101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot find symbol class IOException
提问by user108110
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");
}
}
Whenever i compile the above i get a message saying cannot find symbol class IOException e
每当我编译上述内容时,我都会收到一条消息说找不到符号类 IOException e
can someone tell me how to fix it please :d
有人可以告诉我如何解决它吗:d
thanks
谢谢
采纳答案by Peter Perhá?
IOException is a class from the java.io package, so in order to use it, you should add an import
declaration to your code. import java.io.*;
(at the very top of the java file, between the package name and your class declaration)
IOException 是 java.io 包中的一个类,因此为了使用它,您应该import
在代码中添加一个声明。import java.io.*;
(在 java 文件的最顶部,包名和类声明之间)
FileNotFoundException is aIOException. It's a specialisation of IOException. Once you have caught the IOException, the flow of the program will never get to the point of checking for a more specific IOException. Just swap these two, to test for the more specific case first (FileNotFound) and thenhandle (catch) any other possible IOExceptions.
FileNotFoundException是一个IOException。它是 IOException 的特化。一旦捕获了 IOException,程序流程将永远不会到达检查更具体的 IOException 的地步。只需交换这两个,首先测试更具体的情况(FileNotFound),然后处理(捕获)任何其他可能的 IOExceptions。
回答by TheSoftwareJedi
You need
你需要
import java.io;
at the top of your file.
在文件的顶部。
Also, FileNotFoundException needs to be above IOException since it's a subclass of IOException.
此外,FileNotFoundException 需要高于 IOException,因为它是 IOException 的子类。
回答by thecoop
You need to either import the java.io.* package at the top of the file, or fully-qualify the exception as java.io.IOException
您需要在文件顶部导入 java.io.* 包,或者将异常完全限定为 java.io.IOException
回答by Stephen Denne
Switch the order of FileNotFoundException and IOException
切换 FileNotFoundException 和 IOException 的顺序
回答by bruno conde
Your probably missing an import
reference to IOException
class
. I'ts located in the java.io
package.
您可能缺少import
对IOException
class
. 我位于java.io
包中。
Can I suggest a litle change in you method? Always close the stream in a finally block:
我可以建议你的方法稍微改变一下吗?始终在 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");
}
}