2013-05-11 69 views
0

我们继续运行此错误。目标是将数组中的数据读取到输出文件中。谢谢您的帮助!未报告的异常java.io.FileNotFoundException;必须被捕或宣布被抛出6

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

    } 
+4

好的,那么你对此有何了解?我似乎很清楚。您对检查的异常有多少了解?你真的*需要重定向'System.out'吗? (为什么不直接写入文件,没有重定向?) – 2013-05-11 15:47:25

+0

阅读[关于异常的Java教程](http://docs.oracle.com/javase/tutorial/essential/exceptions/)或Java入门书。 – 2013-05-11 15:51:04

回答

0

你所得到的错误,因为没有与FileOutputStream你没有赶上/ throw -declaring相关的checked exception

+0

具体哪个例外?我现在明白了这个问题,但是我不确定它缺少哪个异常...... – user2373182 2013-05-11 16:05:58

+0

你没有为你的第二个'FileOutputStream()'处理'FileNotFoundException'。 – ValarDohaeris 2013-05-11 16:09:56

+0

@ user2373182:异常消息说:未报告的异常** java.io.FileNotFoundException **;必须被捕获或宣布被抛出。阅读异常消息。 – 2013-05-11 16:47:38

相关问题