Writing at the end of a file via opencsv

I am using opencsv and want to write to the .csv file after several sessions. However, every time I start a new CSVWriter , the old file is erased. Can I change the behavior of CSVWriter to write to the end of the file instead of replacing the file?

+6
java csv opencsv
source share
3 answers

It seems in FileWriter instead of CSVWriter to append to the end of the file.

This code makes it work:

 mFileWriter = new FileWriter(file_path, true); mCsvWriter = new CSVWriter(mFileWriter); 
+24
source share

It is not possible to add a file to opencsv (from the initial look it looks pretty simple), but if you are not limited to opencsv, you can try JExcel . To add to a file in JExcel , you essentially need to create a copy, then work it out and overwrite the original. This may be similar in OpenCSV.

Edit: It seems your only real option is to try JExcel or read the entire file in the list, add to it and write it. If it is too heavy in memory, keep the stream open, read in pieces, write out pieces, and then write out your added piece.

0
source share

It should be possible:

 FileWriter w = new FileWriter("yourfile.csv") CSVWriter writer = new CSVWriter(w, '\t'); ... writer.flush(); CSVWriter writer2 = new CSVWriter(w, '\t'); ... writer2.flush(); w.close(); 

The CSV tool from the H2 database (disclaimer: I wrote) also supports this.

0
source share

All Articles