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 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, andFilesclasses offered by modern Java NIO.2 to make easier work of file-handling. - Use a
BufferedWriterfor 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
BufferedWriterandCSVPrinter. To quote the Javadoc, callingjava.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.
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.
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.
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 - compile "org.apache.commons:commons-csv:1.5" Let's start by generating a simple CSV file — student.csv — in the following program. Our program to generate a simple CSV file: 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 csvP
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.
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%
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 - As with all libraries associated with Apache, it operates with an Apache license, meaning it can be used, distributed and modified freely. Apache Commons allows developers to define their own formats, but offers a predefined set of formats with its CSVFormat class.
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
Google Sites
sites.google.com › view › downloadutyuxm › Apache-Commons-Csv-Writer-Example
Apache Commons Csv Writer Example
ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ ᅠ Select Download Format Apache Commons Csv Writer Example Download Apache Commons Csv Writer Example PDF Download Apache Commons Csv Writer Example DOC Shorter and thrown exception other csv files we do i create carbondata file by apache commons csv
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.
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 - 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.
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.
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVPrinter.html
CSVPrinter (Apache Commons CSV 1.14.2-SNAPSHOT API)
Package org.apache.commons.csv · java.lang.Object · org.apache.commons.csv.CSVPrinter · All Implemented Interfaces: Closeable, Flushable, AutoCloseable · public final class CSVPrinter extends Object implements Flushable, Closeable · Prints values in a CSV format.
Stack Abuse
stackabuse.com › reading-and-writing-csv-files-in-kotlin-with-apache-commons
Reading and Writing CSV Files in Kotlin with Apache Commons
February 27, 2023 - Similar to reading files, we can also write CSV files using Apache Commons. This time around, we'll be using the CSVPrinter. Just how the CSVReader accepts a BufferedReader, the CSVPrinter accepts a BufferedWriter, and the CSVFormat we'd like it to use while writing the file.