在尝试将JTable内容导出到文本文件时获取ArrayIndexOutOfBoundsException:3> = 3

时间:2016-03-01 10:30:17

标签: java jtable

我目前正在尝试为我在Java大学的一个项目编写财务计划。其中一个项目要求包括将我的JTable数据导出到文本文件。这是我的代码(假设该表已经填充)。

但是,当尝试写入文件时,我收到此错误:

java.lang.ArrayIndexOutOfBoundsException:3> = 3

我做错了什么想法......

我最初遵循本指南:http://1bestcsharp.blogspot.co.uk/2015/04/java-io-how-to-export-jtable-data-to-txt-File-In-Java.html

但使用了不同的方法来写入文件(https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html

谢谢,

本。

    try {
    FileWriter fileWriter = new FileWriter(fileName);
    BufferedWriter bw = new BufferedWriter(fileWriter);
    System.out.println(tbl_Finance.getRowCount() + " " + tbl_Finance.getColumnCount());
    for(int i = 0; i < tbl_Finance.getRowCount(); i++) {
       for(int j = 0; i < tbl_Finance.getColumnCount(); j++) {
           bw.write(tbl_Finance.getValueAt(i, j) + " ");
        } 
      bw.write("\n_________\n");
    }
    bw.close();
    JOptionPane.showMessageDialog(null, "Data Exported To " + fileName);
    } catch(Exception ex) {
        ex.printStackTrace();
}

1 个答案:

答案 0 :(得分:1)

变化

for(int j = 0; i < tbl_Finance.getColumnCount(); j++)

for(int j = 0; j < tbl_Finance.getColumnCount(); j++)
相关问题