I have a Jtable which displays the following when triggered.
How can I write all the contents of it to a Text File while keeping its original format. The Text File should look something like this.
Line Number Error Solution Percentage(%)
6 in int 33%
This is what I tried so far. But only value 6 is being written to the file. Any Help.
My codes(only the main parts):
private static final String[] columnNames = {"Line Number", "Error","Solution","Percentage (%)"};
static DefaultTableModel model = new DefaultTableModel(null,columnNames);
public static void DisplayMyJList(List<CaptureErrors> x) throws IOException
{
String [] myErrorDetails = new String[x.size()];
int i = 0;
int line,percentage;
String err, sol;
String aLine;
StringBuffer fileContent = new StringBuffer();
for(CaptureErrors e: x)
{
Vector row = new Vector();
row.add(e.getLinenumber());
row.add(e.getMyfounderror());
row.add(e.getMycorrection());
row.add(e.getMyPercentage()+"%");
model.addRow( row );
for (int i1 = 0; i1 < model.getRowCount(); i1++) {
Object cellValue = model.getValueAt(i1, 0);
// ... continue to read each cell in a row
fileContent.append(cellValue);
// ... continue to append each cell value
FileWriter fileWriter = new FileWriter(new File("C:\\Users\\Antish\\Desktop\\data.txt"));
fileWriter.write(fileContent.toString());
fileWriter.flush();
fileWriter.close();
}
Update I tried this with 2 loops and it gives me the following. I lost the original Format:
Code:
String separator = System.getProperty( "line.separator" );
try
{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true));
PrintWriter fileWriter = new PrintWriter(bufferedWriter);
for(int i1=0; i1<model.getRowCount(); ++i1)
{
for(int j=0; j<model.getColumnCount(); ++j)
{ String names = columnNames[counter];
String s = model.getValueAt(i1,j).toString();
fileWriter.print(names +" ");
fileWriter.append( separator );
fileWriter.print(s + " ");
counter ++;
}
fileWriter.println("");
}
fileWriter.close();
}catch(Exception e)
{