2013-10-03 133 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