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.
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:
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....
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › package-summary.html
org.apache.commons.csv (Apache Commons CSV 1.14.2-SNAPSHOT API)
In addition to individually defined dialects, two predefined dialects (strict-csv, and excel-csv) can be set directly.
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, 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.