0

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

 }});     
}

感谢你给与我的帮助。

4

2 回答 2

3

BufferedWriterthrow 的方法IOException。你必须要么在你的方法体中捕获它,要么声明你的方法来抛出它。

由于您使用的是 的匿名实现ActionListener,因此您无法更改 的签名 actionPerformed。所以你必须抓住IOException里面的actionPerformed

于 2013-10-03T19:00:42.913 回答
0

你应该像这样抓住

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

}

或抛出 IOException

public void actionPerformed(ActionEvent e) throws IOException
于 2013-10-04T07:30:16.507 回答