Java 错误:默认构造函数无法处理异常类型 FileNotFound Exception

发布于 2025-01-06 23:18:59 字数 507 浏览 0 评论 0原文

我正在尝试从文件中读取输入,并将其放入 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));

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

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

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:

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."

I've searched for this error message, but everything I find seems to be unrelated to my situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空袭的梦i 2025-01-13 23:18:59

要么在子类中声明一个抛出 FileNotFoundException 的显式构造函数:

public MySubClass() throws FileNotFoundException {
} 

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

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 小程序,请记住您需要 对其进行签名以便执行IO操作。

Either declare a explicit constructor at your subclass that throws FileNotFoundException:

public MySubClass() throws FileNotFoundException {
} 

Or surround the code in your base class with a try-catch block instead of throwing a FileNotFoundException exception:

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.

只有一腔孤勇 2025-01-13 23:18:59

您需要用 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));

You need to surround your code with try and catch as follows:

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));
舂唻埖巳落 2025-01-13 23:18:59

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

来自 Javadoc:

public FileInputStream(File file) throws FileNotFoundException

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

This is guesswork as we don't have the complete code.

From the 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. This is a checked exception, that you need to either rethrow (i.e. add 'throws FileNotFoundException' in the method where you do the new) or catch (see other try/catch responses).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文