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 OverflowMedium
medium.com › @kaveeshapiumini1999 › data-export-using-apache-commons-csv-09a154243d97
Data Export using Apache Commons CSV | by Kaveeshapiumini | Medium
January 4, 2024 - Here, the List<Person> obtained from the JSON data is passed to the exportToCsv method, which is responsible for converting the Java objects into CSV format. Within this method, Apache Commons CSV is used to create a CSV file writer and printer.
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.
Stack Overflow
stackoverflow.com › questions › 56943093 › organizing-a-csv-file-with-json-datat
java - Organizing a CSV file with JSON datat - Stack Overflow
So CSV should be set up like: headers: name item1 item2 item3 data cells: [first last] [value1] [value2] [value3] JSON looks like this: { "info":[ { "name":{ "first" : ----- "last":----- }, "item": [ { "item1" : "value1" "item2" : "value2" "item3" : "value3" } ] } I have used the Apache commons FileUtils.writeStringToFile, but that writes all the "items" keys and values in one cell, I need 1 item to 1 cell.
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 JIRA
issues.apache.org › jira › browse › CSV-174
[CSV-174] Adding methods to support JSON-CSV interchangeability and adding a class/methods to support direct cell modification - ASF Jira
April 21, 2016 - I shall have the following methods in the class: Constructors: void CSVFile(CSVParser); void CSVFile(Object [][],boolean has_headers); void CSVFile(JSON Array,boolean allow_blanks); void CSVFile(String text,String cell_delimiter,String line_delimiter,boolean force_headerSchema);
GitHub
github.com › TomAlanCarroll › get-json-to-csv › blob › master › src › main › java › com › example › GetJsonToCsv.java
get-json-to-csv/src/main/java/com/example/GetJsonToCsv.java at master · TomAlanCarroll/get-json-to-csv
import org.apache.commons.csv.CSVPrinter; import org.json.JSONArray; import org.json.JSONObject; · import java.io.FileWriter; import java.util.ArrayList; import java.util.List; ·
Author TomAlanCarroll
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".
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.
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 - In this tutorial, we'll go over how to read and write CSV files in Kotlin, using the Apache Commons library, with examples of reading and writing custom objects.