How to convert a List to a CSV file

In this post we will provide an example of how to convert a list to a CSV file in Java. This example is very simple, it uses the standard Java libraries.

What is a CSV file

To answer what is a CSV file, the following quote from Wikipedia puts it perfectly:

In computing, a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format

How to convert a List to a CSV file

The following example does the following:

  • Creates a list of employee objects
  • Create a new file called employee.csv and writes the header line
  • It then loops through the list of objects, converting each employee object to its CSV representation and appends it to the file
  • After running the code the employee.csv file will be populated with your data.

Running the above code produces the following contents in the employee.csv file

Similar posts:

  1. How to read a CSV file
  2. Write list as JSON to file
  3. Java 8 stream allMatch, noneMatch and anyMatch examples
  4. Java ExecutorService examples


  1. Java 8 Files API Documentation
  2. What is a CSV file

Leave a Reply