2013-10-03 132 views
0

我是java的新手,我試圖在按下按鈕時它會更新放入表中的新信息。我收到此錯誤:在actionlistener中未報告的異常java.io.IOException

unreported exception java.io.IOException; must be caught or declared to be thrown 

在這裏,我有麻煩的代碼:

public static void updateAction(){ 

update.addActionListener(new ActionListener() { 


@Override 
public void actionPerformed(ActionEvent e) { 
BufferedWriter bfw = new BufferedWriter(new FileWriter(tmp)); 
for(int i = 0 ; i < table.getColumnCount() ; i++) 
{ 
bfw.write(table.getColumnName(i)); 
bfw.write("\t"); 
} 

for (int i = 0 ; i < table.getRowCount(); i++) 
{ 
bfw.newLine(); 
for(int j = 0 ; j < table.getColumnCount();j++) 
{ 
bfw.write((String)(table.getValueAt(i,j))); 
bfw.write("\t");; 
} 


} 
    bfw.close(); 

}});  
} 

感謝任何幫助,您可以給我。

回答

3

BufferedWriter的方法拋出IOException。你必須在方法體中捕獲它或聲明你的方法拋出它。

由於您使用的是匿名實現ActionListener,因此您無法更改actionPerformed的簽名。所以你必須趕上actionPerformed裏面的IOException

0

你應該抓住這樣的

try { 
    .... 
} catch(IOException e) { 

} 

或拋出IOException異常在

public void actionPerformed(ActionEvent e) throws IOException