未报告的异常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;必须被抓住或宣布被抛出。阅读异常消息。