Question: I have this class containing a method to generate CSV file using *Apache Common CSV library 1.5* 

```js
public class CSVGenerator {

    private static final String CSV_FILE = "./credentials.csv";
    private static CSVPrinter csvPrinter;

    public static void generateCSV(String FirstName, String LastName, String DOB) {

        try {
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(CSV_FILE) );

            csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
                    .withHeader("FirstName", "LastName", "DOB"));

            csvPrinter.printRecord(FirstName, LastName, DOB);
            csvPrinter.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
```

I have a method in my main class , this method will call the method `generateCSV()` a couple of times.  

How can I write a new row and append it to the existing CSV file ? Using my current implementation, it will keep overwriting my first row. 

The more straightforward solution would be to collect all my data in any Java collection (array or list) and then at the very end just iterate the collection and write it to the CSV in one go. But I would not go that way. I prefer to write one row to the CSV , do something else, then call the method again to write a new row and append it to the existing CSV. 

Thanks.

Answer: 
Here is one more easy way. After checking file existence only you should instantiate the Writer object else while instantiating file will get created and every time you will get file.exists() as true.
If file exists you need to create CSVPrinter with withSkipHeaderRecord() else go with any implementation of header() method. FileWriter constructor takes appendable argument with File parameter. If file is there you have to make the appendable parameter as true.

```
File file = new File(filePath.concat("/").concat(fileName));
        if(file.exists()) {
            fileWriter = new FileWriter(file, true);
            csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withSkipHeaderRecord());
        }
        else {
            fileWriter = new FileWriter(file);
            csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader("FirstName", "LastName", "DOB"));

        }

```
Solution after checking the conditions told in <https://stackoverflow.com/a/56034569/3602015>

```
    String data = "stackoverflow";
    File file = new File("tmp/sample.csv");
    BufferedWriter writer;
    CSVPrinter csvPrinter;
    if (!file.exists()) {
        writer = Files.newBufferedWriter(Paths.get("tmp/sample.csv"));
        csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("S No","Col1"));
    } else {
        writer = Files.newBufferedWriter(Paths.get("tmp/sample.csv", StandardOpenOption.APPEND
                , StandardOpenOption.CREATE);
        csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);
    }

    csvPrinter.printRecord("1", data);
    csvPrinter.flush();

```