[英]Java error: Default constructor cannot handle exception type FileNotFound Exception
I'm trying to read input from a file to be taken into a Java applet to be displayed as a Pac-man level, but I need to use something similar to getLine()... So I searched for something similar, and this is the code I found: 我正在尝试从文件中读取输入,以将其输入Java小程序中以显示为吃豆人级别,但是我需要使用类似于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));
The line I marked "ERROR" gives me an error that says "Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor." 我标记为“ ERROR”的行给我一个错误,提示“默认构造函数无法处理隐式超级构造函数抛出的异常类型FileNotFoundException。必须定义一个显式构造函数。”
I've searched for this error message, but everything I find seems to be unrelated to my situation. 我已经搜索了此错误消息,但是我发现的所有内容似乎都与我的情况无关。
Either declare a explicit constructor at your subclass that throws FileNotFoundException
: 在您的子类中声明一个显式构造函数,该构造函数抛出
FileNotFoundException
:
public MySubClass() throws FileNotFoundException {
}
Or surround the code in your base class with a try-catch
block instead of throwing a FileNotFoundException
exception: 或用
try-catch
块将基类中的代码FileNotFoundException
而不是抛出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);
}
}
}
Unrelated, but since you are coding a Java applet, remember that you will need to sign it in order to perform IO operations. 无关,但是由于您正在编写Java applet,因此请记住,您需要对其进行签名才能执行IO操作。
You need to surround your code with try and catch as follows: 您需要使用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));
This is guesswork as we don't have the complete code. 这是猜测,因为我们没有完整的代码。
From the Javadoc: 从Javadoc:
public FileInputStream(File file) throws FileNotFoundException
It means that when you do a new FileInputStream()
like you do, it can come back with a FileNotFoundException
. 这意味着,当您像执行新的
FileInputStream()
一样,它可以返回FileNotFoundException
。 This is a checked exception, that you need to either rethrow (ie add 'throws FileNotFoundException' in the method where you do the new) or catch (see other try/catch responses). 这是一个已检查的异常,您需要重新抛出(即在执行新方法的方法中添加“抛出FileNotFoundException”)或捕获(请参阅其他try / catch响应)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.