【问题标题】:unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown 6未报告的异常 java.io.FileNotFoundException;必须被抓住或宣布被扔掉 6
【发布时间】:2013-05-11 23:53:45
【问题描述】:

我们一直在运行此错误。目标是将数组中的数据读入输出文件。感谢您的帮助!

public static void save(Salesperson[] array)

  {
     PrintStream outfile = null;
     try
     {
        outfile = new PrintStream(new FileOutputStream("data.txt"));
     }
        catch(FileNotFoundException e)
        {
           JOptionPane.showMessageDialog(null,"The file could not be created.");
        }
         System.setOut(new PrintStream(new FileOutputStream("output.txt")));
     for(int k = 0; k < array.length; k++)
     {
        System.out.println(array[k]);
     }

     outfile.close();
     System.out.println("Saved.");

  }

【问题讨论】:

  • 好吧,那你有什么不明白的?这对我来说似乎很清楚。你对受检异常了解多少?你真的需要重定向System.out吗? (为什么不直接写入文件而不进行重定向?)
  • 阅读 Java tutorial about exceptions 或您的 Java 入门书籍。

标签: java filenotfoundexception


【解决方案1】:

您收到错误是因为有一个 checked exception 与您未捕获的 FileOutputStream 相关联/throw-声明。

【讨论】:

  • 具体是哪个异常?我现在明白了这个问题,但我不确定它缺少哪个异常......
  • 你没有为你的第二个new FileOutputStream()处理FileNotFoundException
  • @user2373182:异常消息说:未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被扔掉。阅读异常消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多