[英]unreported exception java.io.IOException at actionlistener
I am new to java and I am trying to make it to when a button is pushed, it will update the new information that was put in the table. 我是Java的新手,我试图使其在按下按钮时生效,它将更新表中放置的新信息。 I am getting this error:
我收到此错误:
unreported exception java.io.IOException; must be caught or declared to be thrown
Here the code I'm having trouble in: 这是我遇到麻烦的代码:
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();
}});
}
Thanks for any help you can give me. 感谢你给与我的帮助。
The methods of BufferedWriter
throw an IOException
. BufferedWriter
的方法抛出IOException
。 You must either catch it in your method body or declare your method to throw it. 您必须将其捕获在方法主体中或声明您的方法以将其抛出。
Since you are using an anonymous implementation of ActionListener
, you can't change the signature of actionPerformed
. 由于您使用的是
ActionListener
的匿名实现,因此无法更改actionPerformed
的签名。 So you must catch the IOException
inside of actionPerformed
. 因此,您必须在
actionPerformed
内部捕获IOException
。
You should catch like this 你应该这样抓
try {
....
} catch(IOException e) {
}
or throws IOException at 或将IOException抛出
public void actionPerformed(ActionEvent e) throws IOException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.