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();
Answer from Arnaud on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › introduction to apache commons csv
Introduction to Apache Commons CSV | Baeldung
January 8, 2024 - FileWriter out = new FileWriter("book_new.csv"); CSVPrinter printer = csvFormat.print(out); We presented the use of Apache’s Commons CSV library through a simple example.
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. 
Discussions

Which CSV Library is Good, Well supported in the Java? Looking for Suggestions?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
14
7
February 9, 2025
Benchmarking every (working) CSV parser for Java in existence.
To put this in perspective, the slowest parser handled 300,000 rows per second. I suppose it's possible for CSV parsing to be a significant part of a CPU-bound workload, but every time I've run into CSV it was ETL-type work and invariably I/O-bound. The stability and usability of a CSV library are going to be much larger factors for me than performance. Except for the time I wrote a horrible CSV-parsing regex. That actually was CPU-bound. ;) More on reddit.com
🌐 r/programming
59
59
October 5, 2014
Trying to learn to read and wirte to CSV files in Java on VSCode, but i keep getting this Exception that i don't know the meaning of.
The error, as noted by u/joranstark018 , is that a request was made, by the instantiation of com.opencsv.CSVParser, to load a class called org.apache.commons.lang3.ObjectUtils. The Java Virtual Machine could not find this class (eg via JARs on the classpath). OpenCSV (scrolling down on this page you can see the direct dependencies listed) depends on Apache Commons Lang. I presume you're including dependencies like OpenCSV by manually adding JARs to the project. Normally we'd use a build tool like Maven or Gradle that manages transitive dependencies for us. Without such a tool, you're going to need to find and add each dependency (and those might have dependencies too). More on reddit.com
🌐 r/javahelp
5
7
September 12, 2022
Best csv parsers for Java. Which one to choose?
Jackson could be good for exposure to the Jackson APIs since they are used quite a bit if you do any spring stuff, but also work great standalone. Need some use cases if you want better answers though. More on reddit.com
🌐 r/javahelp
2
7
May 21, 2022
🌐
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 - A step-by-step guide to reading and writing CSV files in Java using Apache Commons CSV library.
🌐
Javadevcentral
javadevcentral.com › write csv files using apache commons csv
Write CSV Files Using Apache Commons CSV | Java Developer Central
December 24, 2019 - As in the other post, the first step in writing CSV data is to first create a CSVFormat. To read data, we called the parse method on the CSVFormat. To write data, we can call one of the many available print methods.
🌐
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.
🌐
Medium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Within this method, Apache Commons CSV is used to create a CSV file writer and printer. Each Person object’s data is iterated through, and the CSVPrinter is used to write corresponding records to the CSV file.
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › index.html
Overview (Apache Commons CSV 1.14.2-SNAPSHOT API)
Apache Commons CSV reads and writes files in variations of the Comma Separated Value (CSV) format.
🌐
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 - To write this data into file, you just need to create CSVPrinter. And then you can simply start writing records(rows). If you want header row, then make sure you print them above all the data records.
Find elsewhere
🌐
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 - According to the project summary, it attempts to "provide a simple interface for reading and writing CSV files of various types". As with all libraries associated with Apache, it operates with an Apache license, meaning it can be used, distributed and modified freely.
🌐
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 - Reading or writing a CSV file is a very common use-case that Java developers encounter in their day-to-day work. If you need a simple way to read a CSV file or generate a new one for your project then this blog post is for you. In this post, You’ll learn how to read and write CSV files in Java using a very simple open source library called 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 - <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.5</version> </dependency> Or, to add it as a Gradle dependency, you can just add this to your dependencies within the build.gradle file: ... 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 ma
🌐
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%
🌐
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 - Firstly, it writes the CSV file header, and then it writes the students data using CSVPrinter class. ... package com.jcg; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import ...
🌐
javaspring
javaspring.net › blog › java-write-csv-file-with-apache-commons-csv
How to Write and Save CSV Files in Java Using Apache Commons CSV: A Step-by-Step Guide — javaspring.net
Its core classes include CSVFormat (defines CSV rules like delimiters and headers) and CSVPrinter (writes CSV data to an output stream). ... Java Development Kit (JDK) 8 or higher: Apache Commons CSV requires Java 8+.
🌐
Google Sites
sites.google.com › view › downloadutyuxm › Apache-Commons-Csv-Writer-Example
Apache Commons Csv Writer Example
Still no configuration parameters like column to a writer classes. Connected channel on the minimum of a common options. Downstream flume can process it the apache commons writer for reading articles, at a mapping. Restrictions on your data to your parsing csv data to poll.
🌐
Apache Commons
commons.apache.org › proper › commons-csv
Home – 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.
🌐
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVFormat.html
CSVFormat (Apache Commons CSV 1.14.2-SNAPSHOT API)
Builds a new CSVFormat with conversions to and from null for strings on input and output. Reading: Converts strings equal to the given nullString to null when reading records. Writing: Writes null as the given nullString when writing records.
🌐
DevGenius
blog.devgenius.io › java-tutorial-read-and-write-using-apache-commons-csv-d737d43f5765
Java Tutorial: Read and Write Using Apache Commons CSV
August 12, 2025 - Before diving into code, let’s briefly discuss why Apache Commons CSV is a solid choice for handling CSV in Java: Simple API: Easy to learn and use. Support for various CSV dialects: Supports RFC4180, Excel, MySQL, and more. Reliable parsing and escaping: Handles edge cases like embedded commas or quotes. Open-source and actively maintained. Let’s explore how to read and write CSV files using this library.