Writing at the end of a file via opencsv

Viewed 18923

I'm using opencsv and want to write to a .csv file through multiple sessions. However every time I start a new CSVWriter the old file gets erased. Can I change the behavior of the CSVWriter to write at the end of the file instead of replacing the file?

4 Answers

This is possible with OpenCSV, please have a look at the below example to append resultset to the existing csv file.

CSVWriter writer = new CSVWriter(new FileWriter(fileName.concat(".csv"), true), ',');

writer.writeAll(resultSet, true);

The second parameter to the FileWriter constructor is bool to open a file in append mode.

FileWriter(String fileName, boolean append) 
Related