Home » Integration » OpenCSV to read write and parse CSV files

OpenCSV to read write and parse CSV files

This article explains how to use OpenCSV to read, write, and parse CSV files. Open CSV is an open-source tool, we can use this tool to read the data from the CSV files, and write the data into the CSV files.

Why do we need OpenCSV?

We need OpenCSV when we want to import the data into a database from the CSV file and export the data from the database into the CSV file. Here import and export represent the processing of bulk data.

To upload bulk data into the database what we can do?. We have to do the below things.

  • Collect the data in a readable file like CSV.
  • Write the logic on the server-side to read all the data then we process the data and save it into the database.

So obviously we have to write the logic on our side to read the data from the file right!. It's time-consuming to write the logic and all right!. To overcome this we have to use the already built libraries.

There are lots of libraries or tools available to deal with this. One of the best tools is OpenCSV. It's an open-source tool, we can use this for commercial purposes also.

In this article, let us see the below things.

  1. Annotations of OpenCSV.
  2. Reading and Parsing data from a CSV file.
  3. Writing data into a CSV file.

Annotations of OpenCSV

OpenCSV has a more powerful mechanism called annotations that are used to map the bean fields to the header of the CSV file. There are lot of annotations avialbale in OpenCSV that are CsvBindByName, CsvBindByPosition, CsvBindAndSplitByName, CsvBindAndSplitByPosition, and etc..

The two major annotations that involve mapping bean fields are CsvBindByName and CsvBindByPosition. CsvBindByName maps the bean fields name with the column name of the CSV file. CsvBindByPosition maps the beans fields with the column positions like 0,1,2..etc of the CSV file.

The example for both the annotations are below.

The bean ProfileVO has two fields firstName and lastName that are annotated with @CsvBindByName.

public class ProfileVO {
	
        @CsvBindByName
	public String firstName;
	
	@CsvBindByName
	public String lastName;
}

The CSV file is below

firstNamelastName
JohnPeter
Salmankhan

While reading and writing the annotation @CsvBindByName maps the bean fields name with the column name of the CSV file.

Reading and Parsing data from CSV file

There are two ways to read the data from the CSV file. That is reading by header (or) the column name of the CSV file and column positions of the CSV file.

First, we have to enable the OpenCSV by declaring the below maven dependency in the pom.xml file.

<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.6</version>
</dependency>

Here I have used the version of OpenCSV 5.6. Now let's see the reading parts.

Reading the data from the CSV file into an array of strings

We have to do the below things to read the data into an array of strings from a CSV file.

  1. We have to use the Reader to read the CSV file. For example, we can use FileReader or StringReader.
  2. The reader has to be wrapped with CSVReader.

That's it. Then OpenCSV takes care of the rest. Now let us see the examples.

The below example reads the data line by line and stores that into an array of strings.

import java.io.FileReader;
import java.io.IOException;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;

public class ReadIntoArrayOfStrings {
	
	public static void csvReadExample() throws IOException, CsvException{
		try {			
		     //Reading data into array of Strings line by line
		     CSVReader reader = new CSVReader(new FileReader("C:/Users/Desktop/opencsv/profile.csv"));
		     String [] nextLine;
		     while ((nextLine = reader.readNext()) != null) {
		        // nextLine[] is an array of values from the line
		        System.out.println(nextLine[0] +" "+ nextLine[1]+" " + nextLine[2]);
		     }
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	
	}
	public static void main(String[] args) throws IOException, CsvException {		
		csvReadExample();
	}
}

The below table represents the data of the CSV file named 'profile.csv' and located in the path "C:/Users/Desktop/opencsv/profile.csv"

firstNamelastNamequalification
JohnPeterMCA
FazilKhanMBA
SundarRamMTECH

The output for the above program is below

firstName lastName qualification
John Peter MCA
Fazil Khan MBA
Sundar Ram MTECH

The below program reads the data from the CSV file and stores it in the list of strings of an array at one go.

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;

public class ReadIntoArrayOfStrings {
	
	public static void csvReadExample() throws IOException, CsvException{
		try {			
		    //Reading data into array of Strings line by line
		    CSVReader reader = new CSVReader(new FileReader("C:/Users/Desktop/opencsv/profile.csv"));
		    List<String[]> myEntries = reader.readAll();
		    for(String[] myEntry:myEntries)
		    System.out.println(myEntry[0] +" "+ myEntry[1]+" " + myEntry[2]);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	
	}
	public static void main(String[] args) throws IOException, CsvException {		
		csvReadExample();
	}
}

The above program reads the same CSV file and produces the same output as the previous program.

Reading the data from the CSV file into list of beans

We have to do the below things to read the data into a list of beans from a CSV file.

  1. We have to use the Reader to read the CSV file. For example, we can use FileReader or StringReader.
  2. We have to use CsvToBeanBuilder to populate the list of beans from the data of the CSV file.

There are two ways to map the bean fields with CSV data.

  • Map the bean fields with the column name of the CSV file.
  • Map the bean fields with the column position of the CSV file.

The below example reads the data based on the header (or) column name of the CSV file by using the @CsvBindByName annotation.

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.exceptions.CsvException;

public class ReadIntoBeans {

	public static void csvReadExample() throws IOException, CsvException{
		try {			
		//Reading data into list of beans
		List<ProfileVO> profiles = new CsvToBeanBuilder<ProfileVO>(new FileReader("C:/Users/Desktop/opencsv/profile.csv"))
		.withType(ProfileVO.class).build().parse();			
		for(ProfileVO profile:profiles)
		System.out.println(profile.getFirstName()+" "+profile.getLastName()+" "+profile.getQualification());			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}	
	}
	public static void main(String[] args) throws IOException, CsvException {		
		csvReadExample();
	}
}

Bean(ProfileVO)

import com.opencsv.bean.CsvBindByName;

public class ProfileVO {	

	@CsvBindByName
	public String firstName;	

	@CsvBindByName
	public String lastName;	

	@CsvBindByName	
	public String qualification;	

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}
}

The above program reads the same CSV file that we have used previously and produces the output below.

firstName lastName qualification
John Peter MCA
Fazil Khan MBA
Sundar Ram MTECH

If we want to read the CSV file based on the column position then we can use the same program above. But we have to do the below changes in the Bean(ProfileVO).

import com.opencsv.bean.CsvBindByPosition;

public class ProfileVO {	

	@CsvBindByPosition(position = 0)
	public String firstName;	

	@CsvBindByPosition(position = 1)
	public String lastName;	

	@CsvBindByPosition(position = 2)
	public String qualification;	

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}
}

Writing CSV file from an array of Strings

We have to do the below things to write a CSV file from an array of Strings.

  • We have to use the Writer. The writer may be FileWriter or a StringWriter.
  • We have to wrap the Writer with CSVWriter.

That's it. OpenCSV takes care of everything. The example program is below.

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVWriter;

public class WritingCSVFile {
	public static void writeCSVfromArrayOfStrings() {
		try {			
			CSVWriter writer = new CSVWriter(new FileWriter("C:/Users/Desktop/opencsv/profiles.csv"));
			// feed in your array with data
			String[] header = "firstName#lastName#qualification".split("#");
			String[] line1 = "John#Peter#MCA".split("#");
			String[] line2 = "Fazil#Khan#MBA".split("#");
			String[] line3 = "Sundar#Ram#MTECH".split("#");
			List<String[]> entries = new ArrayList<String[]>();
			entries.add(header);
			entries.add(line1);
			entries.add(line2);
			entries.add(line3);
		  
		        for(String[] entry:entries)
		        writer.writeNext(entry);
		        writer.close();		
		}catch(Exception e){
			System.out.println(e.getMessage());
		}	
	}
	public static void main(String[] args)  {		
		writeCSVfromArrayOfStrings();
	}
}

The above program creates the CSV file at the location of "C:/Users/Desktop/opencsv/profiles.csv" and is written with the below data.

firstNamelastNamequalification
JohnPeterMCA
FazilKhanMBA
SundarRamMTECH

Writing CSV file from list of beans

We have to do the below things to write a CSV file from the list of beans.

  • We have to use the Writer. The writer may be FileWriter or a StringWriter.
  • We have to wrap the Writer with CSVWriter.
  • If there is a need then we have to choose the Mapping Strategy otherwise OpenCSV automatically determines one.
  • We have to create the StatefulBeanToCsv and provide a writer for it.

That's it. OpenCSV creates the file and writes the data into it.

While writing no need to mention the mapping strategy. OpenCSV automatically chooses the mapping strategy based on the annotation. For example, if we use @CsvBindByName in the field then OpenCSV chooses the HeaderColumnNameMappingStrategy and if we use @CsvBindByPosition in the field then OpenCSV chooses the ColumnPositionMappingStrategy.

Note: Annotations are not mandatory for writing data into CSV files. If there are no annotations used then OpenCSV chooses the HeaderColumnNameMappingStrategy and uses the fields name as the header name of the CSV file.

The below example writes the data into the CSV file by using the @CsvBindByName annotation.

import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;

public class WritingCSVFile {
	public static void writeCSVfromListOfBeans() {
		try {	
		     ProfileVO profile1 = new ProfileVO("John","Peter","MCA");
		     ProfileVO profile2 = new ProfileVO("Fazil","Khan","MBA");
		     ProfileVO profile3 = new ProfileVO("Sundar","Ram","MTECH");
		     List<ProfileVO> profiles = new ArrayList<ProfileVO>();
		     profiles.add(profile1);
		     profiles.add(profile2);
		     profiles.add(profile3);
		     Writer writer = new FileWriter("C:/Users/Desktop/opencsv/profiles.csv");
		     StatefulBeanToCsv<ProfileVO> beanToCsv = new StatefulBeanToCsvBuilder<ProfileVO>(writer).build();
		     beanToCsv.write(profiles);
		     writer.close();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}	
	}
	public static void main(String[] args)  {		
		writeCSVfromListOfBeans();
	}
}

Bean (ProfileVO)

import com.opencsv.bean.CsvBindByName;

public class ProfileVO {
	
	@CsvBindByName
	public String firstName;
	
	@CsvBindByName
	public String lastName;
	
	@CsvBindByName	
	public String qualification;
	
	public ProfileVO(String firstName, String lastName, String qualification){
		this.firstName = firstName;
		this.lastName = lastName;
		this.qualification = qualification;
	}	

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}	
}

The above program creates the CSV file at the location of "C:/Users/Desktop/opencsv/profiles.csv" and is written with the below data.

FIRSTNAMELASTNAMEQUALIFICATION
JohnPeterMCA
FazilKhanMBA
SundarRamMTECH

We can use the above program for the column position mapping strategy also. But we have to change the annotations of bean fields like below.

import com.opencsv.bean.CsvBindByPosition;

public class ProfileVO {
	
	@CsvBindByPosition(position = 0)
	public String firstName;
	
	@CsvBindByPosition(position = 1)
	public String lastName;
	
	@CsvBindByPosition(position = 2)
	public String qualification;
	
	public ProfileVO(String firstName, String lastName, String qualification){
		this.firstName = firstName;
		this.lastName = lastName;
		this.qualification = qualification;
	}	

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getQualification() {
		return qualification;
	}

	public void setQualification(String qualification) {
		this.qualification = qualification;
	}	
}

The above program produces the below result for the above bean declarations.

JohnPeterMCA
FazilKhanMBA
SundarRamMTECH

I hope you understood the basic things about OpenCSV, Reading data from a CSV file, and Writing data into the CSV file with some basic annotations. In the next article let us see the usage of all the annotations of OpenCSV. Thanks!. Keep Reading!.

Leave a Reply

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