🌐
Apache Commons
commons.apache.org › proper › commons-csv › user-guide.html
User Guide – Apache Commons CSV
Apache Commons, Apache Commons CSV, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
Baeldung
baeldung.com › home › java › java io › introduction to apache commons csv
Introduction to Apache Commons CSV | Baeldung
January 8, 2024 - Apache Commons CSV library has many useful features for creating and reading CSV files. In this quick tutorial, we’ll see how to utilize this library by showing a simple example.
🌐
GitHub
github.com › TransmissionZero › Apache-Commons-CSV-Example
GitHub - TransmissionZero/Apache-Commons-CSV-Example: An example of reading and writing CSV files using the Apache Commons CSV library. · GitHub
An example of reading and writing CSV files using the Apache Commons CSV library. - TransmissionZero/Apache-Commons-CSV-Example
Author   TransmissionZero
🌐
Apache Commons
commons.apache.org › csv
Home – Apache Commons CSV
July 30, 2025 - Apache Commons, Apache Commons CSV, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
CalliCoder
callicoder.com › java-read-write-csv-file-apache-commons-csv
Read / Write CSV files in Java using Apache Commons CSV | CalliCoder
February 18, 2022 - Name,Email,Phone,Country Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India Sachin Tendulkar,sachin@example.com,+91-9999999998,India Barak Obama,barak.obama@example.com,+1-1111111111,United States Donald Trump,donald.trump@example.com,+1-2222222222,United States · In this article, I’ll explain how to read and parse CSV files with a header and without a header using Apache Commons CSV.
🌐
DZone
dzone.com › coding › java › working with csv files in java using apache commons csv
Working With CSV Files in Java Using Apache Commons CSV
April 30, 2018 - Let's start by generating a simple CSV file — student.csv — in the following program. ... import java.io.IOException; import java.io.Writer; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; public class BasicCsvWriter { public static void main(String[] args) { try { //We have to create the CSVPrinter class object Writer writer = Files.newBufferedWriter(Paths.get("student.csv")); CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader("Student Name", "Fees")
🌐
GitHub
github.com › apache › commons-csv
GitHub - apache/commons-csv: Apache Commons CSV · GitHub
The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
Starred by 412 users
Forked by 304 users
Languages   Java 99.8% | Shell 0.2%
🌐
Attacomsian
attacomsian.com › blog › read-write-csv-files-apache-commons-csv
How to read and write CSV files using Apache Commons CSV
September 24, 2022 - ID,Name,Email,Country 1,Atta Shah,atta@example.com,PK 2,Alex Jones,alex@example.com,DE 3,Jovan Lee,jovan@example.com,FR 4,Greg Hover,greg@example.com,US · Let us start with the first file that does not contain a header. There are two ways to read this file which are explained below. The simplest way to read a file through Apache Commons CSV is by using the column index to access the value of a record:
Find elsewhere
🌐
Medium
medium.com › javarevisited › boost-your-productivity-at-csv-files-with-apache-commons-csv-in-java-c52b33037c4c
Boost your Production at CSV files with Apache.commons.CSV in Java
July 18, 2020 - Apache.Commons.CSV offers several methods to read fields at CSV files, in this example, we access the data with header names but it can also be done with index numbers.
🌐
Medium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Data Export using Apache Commons CSV CSV (Comma-Separated Values) files are commonly used for storing tabular data, and Apache Commons CSV makes it easier for Java applications to handle such data in …
🌐
SpringHow
springhow.com › 🏠 › java › apache commons csv to read and write csv files in java
Apache Commons CSV to Read and Write CSV files in Java | SpringHow
June 29, 2021 - Here you need to note a couple of things. The apache-commons CSV library still allows you to access each column using their respective indexes. The header Enums are evaluated in the order of Enum.values() method. Also, All these examples use the DEFAULT CSV format.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › apache › commons › csv
Write/Read CSV Files with Apache Commons CSV Example - Java Code Geeks
November 9, 2023 - In our previous example, we already show how to do that using a native Java code. However, this example will illustrate how to write and read CSV files with an open source 3rd Party tool “Apache Commons CSV”, Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-csvs-in-java-with-apache-commons-csv
Reading and Writing CSVs in Java with Apache Commons CSV
February 20, 2019 - If you want to read a file that ... confusing, Apache Commons also allows the definition of a header for parsing. Instead of using the .withFirstRecordAsHeader() method when defining the format of the CSV file, you can define a header manually. For example, if you want to avoid ...
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › index.html
Overview (Apache Commons CSV 1.14.2-SNAPSHOT API)
public enum Headers { ID, CustomerNo, Name } Reader in = new FileReader("path/to/file.csv"); Iterable<CSVRecord> records = CSVFormat.RFC4180.builder() .setHeader(Headers.class) .build() .parse(in); for (CSVRecord record : records) { String id = record.get(Headers.ID); String customerNo = ...
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVFormat.html
CSVFormat (Apache Commons CSV 1.14.2-SNAPSHOT API)
Use CSVFormat.Builder.setComme... is not set, then the header comments are ignored. For example: builder.setCommentMarker('#').setHeaderComments("Generated by Apache Commons CSV", Instant.ofEpochMilli(0)); writes: # Generated by Apache Commons CSV....
🌐
Javadevcentral
javadevcentral.com › write csv files using apache commons csv
Write CSV Files Using Apache Commons CSV | Java Developer Central
December 24, 2019 - Apache Commons CSV is one of the components in the Apache Commons project. We can use Apache Commons CSV to read and write Comma Separated Value (CSV) files. In one of the earlier posts, I have shown how to read CSV files using Apache Commons CSV.
🌐
Maven Repository
mvnrepository.com › artifact › org.apache.commons › commons-csv
Maven Repository: org.apache.commons » commons-csv
July 27, 2025 - The Apache Commons CSV library provides a simple interface for reading and writing CSV files of various types. ... aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript ...
Top answer
1 of 3
17

It looks like you are printing all the records on the same line .

Other methods like printRecords will be more helpful :

String outputFile = savePath+".csv";
CSVPrinter csvFilePrinter = null;
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
FileWriter fileWriter = new FileWriter(outputFile);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

csvFilePrinter.printRecords(excelParser.getRecords());


fileWriter.flush();
fileWriter.close();
csvFilePrinter.close();
2 of 3
11

Automatically close & flush

The Answer by Arnaud is correct and good. Here is a variation, shorter and more modern.

Here we:

  • Use the Path, File, and Files classes offered by modern Java NIO.2 to make easier work of file-handling.
  • Use a BufferedWriter for better performance with large amounts of data.
  • Specify the character encoding to be used. Usually UTF-8 is the best. If you do not understand, read this.
  • Include the necessary try-catches for file-related exceptions.
  • Add try-with-resources syntax to auto-close the file.
  • Skip the explicit flushing, as the buffered writer will be flushed automatically as part of auto-closing the BufferedWriter and CSVPrinter. To quote the Javadoc, calling java.io.Writer::close “Closes the stream, flushing it first.”.

Code:

CSVFormat format = CSVFormat.EXCEL.withHeader();
Path path = Paths.get( savePath + ".csv" );
try 
(
    BufferedWriter writer = Files.newBufferedWriter( path , StandardCharsets.UTF_8 ) ;
    CSVPrinter printer = new CSVPrinter( writer , format ) ;
)
{
    printer.printRecords( excelParser.getRecords() );
} 
catch ( IOException e )
{
    e.printStackTrace();
}
// At this point, the `CSVPrinter` is automatically closed. 
// And, ➡️ the `BufferedWriter` is automatically closed and flushed.