How to merge multiple csv files into one in Java

This tutorial shows how to merge multiple csv files into one in Java.

Suppose there are n number of csv files and each csv files having different number of headers also; so this example will show you how to merge all these csv files into one file and write all the data to a single csv file.
Let’s say we have two csv files csv1.csv and csv2.csv with the following data

csv1.csv

 

csv2.csv

 

So if we merge the above two files then it should look like

single.csv

 

Look at the merged file single.csv which has only unique headers but data from both the files csv1.csv and csv2.csv.

Now look at the below java code how I am doing it.

I am using opencsv-2.2.jar file for reading from and writing to the csv files. You can download it easily or you can download the source code from the link at the end of this tutorial.

Define the model how the csv file each data will look like. Each csv file may have different number of headers along with data so I have used here Map for putting the key/value pair so that it will be easier to retrieve later.

 

Create the CSVParser.java which will actually do the neccessary processsing. Here getCSVHeaders(File file) returns all the headers from a file and getAllCSVHeaders(List<File> files) merges all headers returned for each file from getCSVHeaders(File file). I have used Set for return type in getAllCSVHeaders(List<File> files) so that I get the unique headers for the merged csv file. getAllRecords(File file, List<String> keys) returns a list of Record with each Record is kept in a map. The method writeToSingleFile(File output, List<Record> records,
Set<String> keys) writes to a single file.

 

That’s all. Thank you for your patience. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.