Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被扔_Java_Filenotfoundexception - Fatal编程技术网

未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被扔

未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被扔,java,filenotfoundexception,Java,Filenotfoundexception,我们一直在运行这个错误。目标是将数组中的数据读取到输出文件中。谢谢你的帮助 public static void save(Salesperson[] array) { PrintStream outfile = null; try { outfile = new PrintStream(new FileOutputStream("data.txt")); } catch(FileNotFoundException e

我们一直在运行这个错误。目标是将数组中的数据读取到输出文件中。谢谢你的帮助

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

  }
公共静态作废保存(Salesperson[]数组)
{
PrintStream outfile=null;
尝试
{
outfile=new PrintStream(new FileOutputStream(“data.txt”);
}
catch(filenotfounde异常)
{
showMessageDialog(null,“无法创建文件”);
}
系统设置(新的PrintStream(新的FileOutputStream(“output.txt”));
for(int k=0;k
您之所以会收到错误,是因为您没有捕捉到与之关联的抛出-声明。

好的,那么您不了解它的哪些方面?我似乎很清楚。您对检查异常了解多少?你真的需要重定向系统输出吗?(为什么不直接写入文件,而不重定向?)阅读,或者您的Java入门书。具体是哪一个例外?我现在理解了这个问题,但我不确定它缺少了哪个异常…您没有为第二个
新FileOutputStream()处理
FileNotFoundException
:异常消息说:未报告的异常java.io.FileNotFoundException;必须被抓住或宣布被抛出。阅读异常消息。