3

Is it possible to add new lines to a CSV file, rather than overwrite the last one?

Here is my method I call when I want to add a new line:

private static void writeWithCsvMapWriter() throws Exception 
{
    final String[] header = new String[] { "Engineer", "Time/Date", "Project", "Test ID", "Data Centre", "Data Hall", "Row", "Grille", "I/S" };

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss");
    String currentDateandTime = sdf.format(new Date());

    final Map<String, Object> NewRow = new HashMap<String, Object>();
    NewRow.put(header[0], Name.getText().toString());
    NewRow.put(header[1], currentDateandTime);
    NewRow.put(header[2], "Expect");
    NewRow.put(header[3], TestID.getText());
    NewRow.put(header[4], DataCentre.getText());
    NewRow.put(header[5], DataHall.getText());
    NewRow.put(header[6], Row.getText());
    NewRow.put(header[7], Grille.getText());
    NewRow.put(header[8], IS.getText());

    ICsvMapWriter mapWriter = null;

    try 
    {
        mapWriter = new CsvMapWriter(new FileWriter("data/data/" + PACKAGE_NAME +"/writeWithCsvMapWriter.csv"), CsvPreference.STANDARD_PREFERENCE);

        final CellProcessor[] processors = getProcessors();

        mapWriter.writeHeader(header);
        mapWriter.write(NewRow, header, processors);  
    }
    finally 
    {
        if( mapWriter != null ) 
        {
            mapWriter.close();
        }
    }
}

Thanks

dev6546
  • 1,412
  • 7
  • 21
  • 40

2 Answers2

6

Add true to the parameters in new FileWriter:

mapWriter = new CsvMapWriter(new FileWriter(".../writeWithCsvMapWriter.csv", true), CsvPreference.STANDARD_PREFERENCE);
hd1
  • 33,938
  • 5
  • 80
  • 91
frickskit
  • 624
  • 1
  • 8
  • 19
  • Perfect, I should of tried this, I'm sure this is how you do it in .NET. – dev6546 Mar 31 '13 at 14:15
  • 1
    Do note that every time you run this, it will also cause the possible header line to be outputted and appended to the file again – Forage Oct 20 '16 at 13:24
1

As frickskit pointed out: the trick is to add true as the second parameter of new FileWriter(), but this will add the header underneath existing rows again as well. Use something in the lines of the following to check for the existence of a header in an existing file first before you decide to write it out.

public void write(String fileName, List<Map<String, String>> lines) throws IOException {
    final String[] HEADER = new String[] {
        "Column1",
        "Column2",
        "Column3"
    };

    boolean writeHeader = true;

    try (ICsvMapReader mapReader = new CsvMapReader(new FileReader(fileName), CsvPreference.STANDARD_PREFERENCE)) {

        final String[] existingHeader = mapReader.getHeader(true);

        if (Arrays.equals(existingHeader, HEADER)) {
            writeHeader = false;
        }
    } catch (FileNotFoundException ex) {}

    try (ICsvMapWriter mapWriter = new CsvMapWriter(new FileWriter(fileName, true), CsvPreference.STANDARD_PREFERENCE)) {

        final CellProcessor[] processors = getProcessors();

        if (writeHeader) {
            mapWriter.writeHeader(HEADER);
        }

        for (Map<String, String> entry : lines) {
            mapWriter.write(entry, HEADER, processors);
        }
    }
}
Forage
  • 2,726
  • 2
  • 18
  • 20
  • if you write a lot of csv-records, this can be resource-consuming. reading every time before writing, but one can write a separate method only for writing the header at the beginning – jimmybondy Mar 21 '19 at 09:07