0

I want to export data from JTable to a file in CSV format. For read all data in TableModel i've used this:

for (int row = 0; row < rowCount; row++) {
  for (int col = 0; col < columnCount; col++) {
    csvData += tableModel.getValueAt(row, col);
    csvData += col + 1 < columnCount ? "," : "";
  }
  csvData += "\r\n";
}

But for a large dataset the process is too slow. And i think the reason is not the loop itself, but getValueAt method that is slow.

So I ask if there is a faster way to get data from TableModel?

Daniel Silva
  • 316
  • 1
  • 5
  • 10
  • 3
    Use a StringBuilder,not a String. – Hovercraft Full Of Eels Sep 07 '15 at 02:47
  • Or, if you're using Java 8, `StringJoiner` – MadProgrammer Sep 07 '15 at 02:47
  • You might also consider storing the values in your `TableModel` implementation which is more efficient for your needs – MadProgrammer Sep 07 '15 at 02:49
  • Don't create one large String before saving the data to the file. Write out each row of data individually, then you will have far less memory requirements and you should be able to handle a table of any size. – camickr Sep 07 '15 at 02:58
  • That's it. StringBuilder is incredibly more faster than concatenate strings. Sorry for duplicate question. My view of the problem was not correct. I didn't know about the problem with concatenate method. My bad. Best regards. – Daniel Silva Sep 07 '15 at 03:36

0 Answers0