import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
private void btnFileSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\CedulaList.txt"));
for (int i = 0 ; i < tableCedula.getRowCount(); i++)
{
for(int j = 0 ; j < tableCedula.getColumnCount();j++)
{
bfw.newLine();
bfw.write((String)(tableCedula.getValueAt(i,j)));
bfw.write("\t");;
}
}
bfw.close();
}
我似乎没有掌握如何解决这个问题。错误说,&#34;未报告的异常IOException;必须被抓或宣布被抛出。
转换为try-with-resources。&#34;
答案 0 :(得分:0)
将throws
子句添加到方法签名
private void btnFileSaveActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
或使用try catch
块
private void btnFileSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\CedulaList.txt"));
for (int i = 0 ; i < tableCedula.getRowCount(); i++)
{
for(int j = 0 ; j < tableCedula.getColumnCount();j++)
{
bfw.newLine();
bfw.write((String)(tableCedula.getValueAt(i,j)));
bfw.write("\t");;
}
}
bfw.close();
} catch(IOException e){
//Handle the exception
}
}
答案 1 :(得分:0)
你需要抛出/ try-catch为:
private void btnFileSaveActionPerformed(java.awt.event.ActionEvent evt)
{
try{
BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\CedulaList.txt"));
for (int i = 0 ; i < tableCedula.getRowCount(); i++)
{
for(int j = 0 ; j < tableCedula.getColumnCount();j++)
{
bfw.newLine();
bfw.write((String)(tableCedula.getValueAt(i,j)));
bfw.write("\t");;
}
}
}catch(Exception ex){
}finally{
if(bfw != null){
bfw.close();
}
}
}