actionlistener中未报告的异常java.io.IOException

时间:2013-10-03 18:56:14

标签: java file-io ioexception

我是java的新手,我试图在按下按钮的时候,它会更新放在表格中的新信息。我收到了这个错误:

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();

 }});     
}

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:3)

BufferedWriter的方法抛出IOException。您必须在方法体中捕获它或声明方法以抛出它。

由于您使用的是ActionListener的匿名实施,因此无法更改actionPerformed的签名。因此,您必须抓住IOException内的actionPerformed

答案 1 :(得分:0)

你应该像这样抓住

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

}

或在

处抛出IOException
public void actionPerformed(ActionEvent e) throws IOException
相关问题