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 OverflowAttacomsian
attacomsian.com › blog › read-write-csv-files-apache-commons-csv
How to read and write CSV files using Apache Commons CSV
September 24, 2022 - However, if you use a CSV file as a simple text file to transfer the data from one server to another, the file may not include the header. The Apache Commons CSV library works in both cases. Let us create two sample CSV files: one with a header and another without a header. We will use these files to read and parse in our examples.
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.
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 › 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 › 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 - CSV (comma-separated values) files are used widely to exchange data between applications. However, operations with CSV files can be tricky and time-consuming. Apache Software Foundation gives us the Apache.Commons.CSV library that makes our life easier with CSV files READ/WRITE operations. Thus it comes with Apache License 2.0. CSV files have various formats, in the following example ...
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
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.
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 - The Apache Commons CSV library is the Apache Software Foundation's version of a Java CSV parser. According to the project summary, it attempts to "provide a simple interface for reading and writing CSV files of various types".
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 - So, Let’s see how we can write and read a simple CSV file using the Apache Commons CSV. The latest stable release of Commons CSV is 1.0, we can download it from here or we can pull it from the central Maven repositories using the following dependency in your project POM: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.0</version> </dependency> We create this simple POJO to contain the student data like id, firstName, lastName, gender and age.
Google Sites
sites.google.com › view › downloadutyuxm › Apache-Commons-Csv-Writer-Example
Apache Commons Csv Writer Example
Always be specified number of basic classes in data conforms to get written out to the commons writer in using a reader capable of usability of book. Unescapes any double quotes, i recently faced with the size of pooled parsers should the example configuration. Customization writer output writer is similar and updated regularly, you want to be the split each time. So any other ooxml and ppt as a csv file on the apache commons csv for overwriting the jira project!
Simplesolution
simplesolution.dev › java-write-read-csv-file-using-apache-commons-csv
Write and Read CSV File in Java using Apache Commons CSV
To download the Apache Commons CSV jar file you can visit Apache Commons CSV download page at commons.apache.org · import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import java.io.FileWriter; import java.io.IOException; public class WriteCsvFileExample { public static void main(String...
Apache Commons
commons.apache.org › csv › apidocs › org › apache › commons › csv › CSVPrinter.html
CSVPrinter (Apache Commons CSV 1.14.2-SNAPSHOT API)
October 23, 2021 - Furthermore printRecords(Object...), printRecords(Iterable) and printRecords(ResultSet) methods can be used to print several records at once. Example: try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.EXCEL)) { printer.printRecord("id", "userName", "firstName", "lastName", "birthday"); printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15)); printer.println(); printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29)); } catch (IOException ex) { ex.printStackTrace(); } This code will write the following to csv.txt: id,userName,firstName,lastName,birthday 1,john73,John,Doe,1973-09-15 2,mary,Mary,Meyer,1985-03-29 ·
GitHub
gist.github.com › nicdoye › 7709502
Quick example of using Apache Commons CSV (1.0-Snapshot) to write to a CSV file in groovy. · GitHub
Quick example of using Apache Commons CSV (1.0-Snapshot) to write to a CSV file in groovy. - csvwriter.groovy
Blogger
albert-kuo.blogspot.com › 2017 › 06 › how-to-use-apache-commons-csv-to.html
albert's blog: How to use Apache Commons CSV to Read/Write CSV file
package albert.practice.csv; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVRecord; import lombok.extern.slf4j.Slf4j; @Slf4j public class CSVTest { // Delimiter used in CSV file private static final String NEW_LINE_SEPARATOR = "\n"; // CSV file header private static final String[] FILE_HEADER = {"ns", "identifier"}; // OpcVo attri