2013-05-11 84 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

相關問題