import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.csvreader.CsvWriter; public class CsvWriterAppendExample { public static void main(String[] args) { String outputFile = "users.csv"; // before we open the file check to see if it already exists boolean alreadyExists = new File(outputFile).exists(); try { // use FileWriter constructor that specifies open for appending CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ','); // if the file didn't already exist then we need to write out the header line if (!alreadyExists) { csvOutput.write("id"); csvOutput.write("name"); csvOutput.endRecord(); } // else assume that the file already has the correct header line // write out a few records csvOutput.write("1"); csvOutput.write("Bruce"); csvOutput.endRecord(); csvOutput.write("2"); csvOutput.write("John"); csvOutput.endRecord(); csvOutput.close(); } catch (IOException e) { e.printStackTrace(); } } } /* Output Format: ------------- id,name 1,Bruce 2,John */