-1

I have a Jtable which displays the following when triggered.

Jtable

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)
      {

enter image description here

4

1 回答 1

3

Your code isn't complete but I can see 1, maybe 2 errors:

1) You need a double loop, one for the rows and then a second for every column in the row. The code you posted only shows you getting the value from the first column which would explain why you only see "6".

2) The code to write to the file needs to be outside your two loops. The way the code is written now you will recreate a new file for every row, which mean you will only ever have a single row of data in the file

于 2013-03-02T05:36:44.387 回答