Java 错误:未报告的异常 FileNotFoundException;必须被捕获或声明被抛出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19788989/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:13:44  来源:igfitidea点击:

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

javaprintwriter

提问by user2956248

I'm trying to create a simple program that will output a string to a text file. Using code I found here, I have put together the following code:

我正在尝试创建一个将字符串输出到文本文件的简单程序。使用我在这里找到的代码,我将以下代码放在一起:

import java.io.*;

public class Testing {

  public static void main(String[] args) {

    File file = new File ("file.txt");
    file.getParentFile().mkdirs();

    PrintWriter printWriter = new PrintWriter(file);
    printWriter.println ("hello");
    printWriter.close();       
  }
} 

J-grasp throws me the following error:

J-grasp 向我抛出以下错误:

 ----jGRASP exec: javac -g Testing.java

Testing.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    PrintWriter printWriter = new PrintWriter(file);
                              ^
1 error

 ----jGRASP wedge2: exit code for process is 1.

Since I'm pretty new to Java, I have no idea what this means. Can anybody point me in the right direction?

因为我对 Java 还很陌生,所以我不知道这意味着什么。有人能指出我正确的方向吗?

采纳答案by 06needhamt

You are not telling the compiler that there is a chance to throw a FileNotFoundExceptiona FileNotFoundExceptionwill be thrown if the file does not exist.

您不是在告诉编译器,如果文件不存在,则有机会抛出FileNotFoundExceptionaFileNotFoundException将被抛出。

try this

尝试这个

public static void main(String[] args) throws FileNotFoundException {
    File file = new File ("file.txt");
    file.getParentFile().mkdirs();
    try
    {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println ("hello");
        printWriter.close();       
    }
    catch (FileNotFoundException ex)  
    {
        // insert code to run when exception occurs
    }
}

回答by Thirumalai Parthasarathi

a PrintWritermight throw an exception if there is something wrong with the file, like if the file doesn't exist. so you have to add

PrintWriter如果文件有问题,a可能会抛出异常,比如文件不存在。所以你必须添加

public static void main(String[] args) throws FileNotFoundException {

then it will compile and use a try..catchclause to catch and process the exception.

然后它将编译并使用try..catch子句来捕获和处理异常。

回答by SQB

This means that when you call new PrintWriter(file), it can throw an exception. You either need to handle that exception, or make your program able to rethrow it.

这意味着当您调用 时new PrintWriter(file),它可能会抛出异常。您要么需要处理该异常,要么使您的程序能够重新抛出它。

import java.io.*;

public class Testing {

    public static void main(String[] args) {

    File file = new File ("file.txt");
    file.getParentFile().mkdirs();

    PrintWriter printWriter;
    try {
        printwriter = new PrintWriter(file);
        printWriter.println ("hello");
        printWriter.close();
    } catch (FileNotFoundException fnfe) {
        // Do something useful with that error
        // For example:
        System.out.println(fnfe);
    }
}

回答by jon.nicholssoftware.com

If you're very new to Java, and just trying to learn how to use PrintWriter, here is some bare-bones code:

如果您对 Java 非常陌生,并且只是想学习如何使用PrintWriter,这里有一些基本代码:

import java.io.*;

public class SimpleFile {
    public static void main (String[] args) throws IOException {
        PrintWriter writeMe = new PrintWriter("newFIle.txt");
        writeMe.println("Just writing some text to print to your file ");
        writeMe.close();
    }
}