Read write CSV file in Java – OpenCSV tutorial

CSV stands for ‘comma-separated values‘. CSV files are mostly used for creating data files either for exporting the data OR importing the data. Java language does not provide any native support for effectively handling CSV files. Without using 3rd party libraries, we may end up creating your own CSV parser. There is usually no advantage in re-inventing the wheel, So it is advisable to use such 3rd party tools for parsing the CSV files.

OpenCSV is such a tool which can be used to read a csv file in java or write data to CSV file.

Table of Contents

1. OpenCSV maven dependencies
2. OpenCSV common classes
3. Read a CSV file
4. Write a new CSV file
5. Append to an existing CSV file
6. Custom separator for CSV file
7. Read CSV to Java beans
8. Create CSV from SQL data

1. OpenCSV maven dependencies

If we are working on a maven project, we can include the OpenCSV maven dependency in pom.xml file like below.

<dependency>
	<groupId>net.sf.opencsv</groupId>
	<artifactId>opencsv</artifactId>
	<version>2.3</version>
</dependency>

We can download the binaries/jars from https://sourceforge.net/projects/opencsv/ if you are not using maven project.

2. OpenCSV common classes

Below is list of most used OpenCSV classes which we should go through.

  • CSVParser : A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.
  • CSVReader : You will be using this class most of the times while reading a CSV file from your java application code. This class provides a number of useful constructors to build CSVReader with different options and capabilities. e.g. you can supply a different separator character (default in comma), a different quote character (default is double-quote) and even you can supply the initial line number from where parsing should start.
  • CSVWriter : CSVWriter is also very customizable just like CSVReader. You can use custom separator, custom quote character or custom line terminator while writing a CSV file using your java application code.
  • CsvToBean : This class will be used when you want to populate your java beans from a CSV file content. You will see an example below.
  • BeanToCsv : If you want to export data to CSV file from your java application, you might need help of this class as well.
  • ColumnPositionMappingStrategy : If you plan to use CsvToBean (or BeanToCsv) for importing CSV data, you will use this class to map CSV fields to java bean fields.

3. How to read a CSV file

3.1. Read CSV file line by line

As mentioned above, to read a CSV file we will take help of CSVReader class. Let’s look at a quick example for reading a CSV file line by line.

import java.io.FileReader;
import java.util.Arrays;

import au.com.bytecode.opencsv.CSVReader;

public class ParseCSVLineByLine
{
   @SuppressWarnings("resource")
   public static void main(String[] args) throws Exception
   {
      //Build reader instance
	  //Read data.csv
	  //Default seperator is comma
	  //Default quote character is double quote
	  //Start reading from line number 2 (line numbers start from zero)
      CSVReader reader = new CSVReader(new FileReader("data.csv"), ',' , '"' , 1);
      
      //Read CSV line by line and use the string array as you want
      String[] nextLine;
      while ((nextLine = reader.readNext()) != null) {
         if (nextLine != null) {
			//Verifying the read data here
            System.out.println(Arrays.toString(nextLine));
         }
       }
   }
}

3.2. Read complete CSV file and iterate line by line

Above example read the CSV file line by line and print to console. We can read the complete CSV file once and then iterate over data as we want. Below is example of build CSV data read using readAll() method.

import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class ParseFullCSVExample
{
   @SuppressWarnings("resource")
   public static void main(String[] args) throws Exception
   {
      //Build reader instance
      CSVReader reader = new CSVReader(new FileReader("data.csv"), ',', '"', 1);
      
      //Read all rows at once
      List<String[]> allRows = reader.readAll();
      
      //Read CSV line by line and use the string array as you want
     for(String[] row : allRows){
        System.out.println(Arrays.toString(row));
     }
   }
}

In above example, we read the whole CSV file in one go and then we are iterating over rows one by one.

4. How to write a CSV file

Writing a CSV file is as simple as reading it. Create an instance of CSVWriter with appropriate configuration options and start writing data to CSV file. Let’s look at the example.

import java.io.FileWriter;

import au.com.bytecode.opencsv.CSVWriter;

public class WritingCSVFileExample
{
   public static void main(String[] args) throws Exception
   {
      String csv = "data.csv";
      CSVWriter writer = new CSVWriter(new FileWriter(csv));
       
	  //Create record
      String [] record = "4,David,Miller,Australia,30".split(",");
      //Write the record to file
      writer.writeNext(record);
       
	  //close the writer
      writer.close();
   }
}

5. How to append to an existing CSV file

Above example creates a new CSV file and start writing data from start i.e. line number 0. Many times we want to append data to existing CSV file instead of writing a new file. We can achieve this functionality by passing a second argument to FileWriter instance.

There is no direct support for appending in OpenCSV library.

import java.io.FileWriter;

import au.com.bytecode.opencsv.CSVWriter;

public class AppendToCSVExample
{
   public static void main(String[] args) throws Exception
   {
      String csv = "data.csv";
      CSVWriter writer = new CSVWriter(new FileWriter(csv, true));
       
      String [] record = "3,David,Feezor,USA,40".split(",");
       
      writer.writeNext(record);
       
      writer.close();
   }
}

6. Custom separator for CSV file

Custom separator can be specified in constructor of CSVReader or CSVWriter. Let’s look at an example for it. I have changed the default separator from comma to semi-colon.

import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class CustomSeperatorExample
{
   @SuppressWarnings("resource")
   public static void main(String[] args) throws Exception
   {
      //Build reader instance
      CSVReader reader = new CSVReader(new FileReader("data.csv"), ';', '"', 1);
      
      //Read all rows at once
      List<String[]> allRows = reader.readAll();
      
      //Read CSV line by line and use the string array as you want
     for(String[] row : allRows){
        System.out.println(Arrays.toString(row));
     }
   }
}

7. Read CSV to Java beans

OpenCSV also provides functionality to read CSV file to Java beans directly. Let’s build a quick example for it as well.

7.1. CSV file

1,Lokesh,Gupta,India,32
2,David,Miller,England,34

7.2. Java bean

public class Employee implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   private String id;
   private String firstName;
   private String lastName;
   private String country;
   private String age;
   
   //Getters and setters
   
   @Override
   public String toString()
   {
      return "Employee [id=" + id + ", firstName=" + firstName + ", 
				lastName=" + lastName + ", country=" + country + ", age=" + age + "]";
   }
}

7.3. Read CSV to Java bean

Below example read the data.csv file and populate the instances of Employee.java. Then it verify the data by printing it on console.

import java.io.FileReader;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;

public class CSVMappedToJavaBeanExample
{
   @SuppressWarnings({"rawtypes", "unchecked"})
   public static void main(String[] args) throws Exception
   {
      CsvToBean csv = new CsvToBean();
      
      String csvFilename = "data.csv";
      CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
      
	  //Set column mapping strategy
      List list = csv.parse(setColumMapping(), csvReader);
	  
      for (Object object : list) {
          Employee employee = (Employee) object;
          System.out.println(employee);
      }
   }
   
   @SuppressWarnings({"rawtypes", "unchecked"})
   private static ColumnPositionMappingStrategy setColumMapping()
   {
      ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
      strategy.setType(Employee.class);
      String[] columns = new String[] {"id", "firstName", "lastName", "country", "age"}; 
      strategy.setColumnMapping(columns);
      return strategy;
   }
}

Program Output.

Employee [id=1, firstName=Lokesh, lastName=Gupta, country=India, age=32]
Employee [id=2, firstName=David, lastName=Miller, country=England, age=34]

8. Create CSV from SQL data

Now this is also a desired activity we will need in the applications. For exporting data directly from SQL tables to CSV file, we will need the ResultSet object. Following API can be used to write data to CSV from ResultSet using obtained ResultSet reference.

java.sql.ResultSet myResultSet = getResultSetFromSomewhere();

writer.writeAll(myResultSet, includeHeaders); //writer is instance of CSVWriter

In above method, the first argument is the ResultSet which we want to write to CSV file. And the second argument is boolean which represents whether we want to write header columns (table column names) to file or not.

That’s all for explaining the basic usages of OpenCSV library to read and write CSV file from the java application code. Share your thoughts and queries in comment section.

Happy Learning !!

Read More :

How to read write excel files in Java – Apache POI tutorial
Create PDF files using java – iText Tutorial

References:

http://mvnrepository.com/artifact/net.sf.opencsv/opencsv
http://opencsv.sourceforge.net/apidocs/index.html

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

21 thoughts on “Read write CSV file in Java – OpenCSV tutorial”

  1. I am new to Selenium Webdriver Testng and trying to Develop Data Driven framework using CSVReader

    I have 5 tests in Testng and have to run those all on different environments. So basically I have to pick a different url and client, but other pages, elements remain same.

    My Question is, Where do I put while loop so that it reads first line and execute all the tests and then read second line and so on.

    I tried initialize CSV reader at class level, before suite, before method etc. but it throws error, it works for a particular test if I am putting insside @Test but that doesn’t resolve the purpose, I couldn’t find any relevant answer on the internet.

    Reply
  2. Please note that for Android, mapping to a java bean does not work due to Android missing the class “java.beans.Introspector”. You can use reader.readAll() and then take it from there.

    Reply
  3. CSVReader reader = new CSVReader(new FileReader(“data.csv”), ‘,’ , ‘”‘ , 1);

    reader.readNext() doesn’t work properly when one of the columns data has special characters as input such as below

    “~!@#$%^&*()_+`[]{}|:””;,./{}|{}|+*&%#@~\”

    Reply
  4. Good tutorial, seems pretty clearly organized. I would however, have 2 questions for you based on this material:
    1. Would it possible to apply the same mapping from CSV directly to Java bean in case of having non-String fields? For example if the employee ID and the Employee age were declared integers, would the same mapping presented here be valid?
    2. How would it be possible to export data using OpenCSV into HTML format?

    Reply
  5. what is the parse method in class CSVMappedToJavaBeanExample.java as it is not defined in Employee class.
    List list = csv.parse(setColumMapping(), csvReader);

    Reply
  6. Hello Sir,

    This information is very useful. However, in my requirement, I need to ignore the occurrence of delimiter in the last string I get from CSV. Can you give an example how it could be done?

    Thanks & regards,
    Santhosh Kumar.

    Reply
      • First Name;;; Last Name; City; Address;; zip code;; and this \n is the \n additional; text; as \n a last string

        Thank you for your reply Lokesh. Consider the above row as text in the CSV file. In this case, using “;” as delimiter, I can get the strings. But in the last string mentioned in brackets, (and this \n is the \n additional; text; as \n a last string), I dont want the delimition to happen. This is what I am looking for. In this case, I cannot use “\” as escape character as \n would not work. Please help. Thanks in advance

        Reply
        • You can do only those things with code which you logically implement in steps. In your case, probably you first need to use the delimiter ‘and’ or any such word. This will divide you string into two parts. You can again tokenize the first part with delimiter ‘;’ and leave the second part.

          If you can not break your logic into logical steps, you will not be able to write the code for it as well. So please try to create logical steps first, and then let me know for which part you want me to help in writing code.

          I hope I make sense.

          Reply
  7. Hi,

    I would like to write mutiple resultsets into a csv file. How can I do that? Is there anyway I can write in next tabs?

    Please advice.

    Thanks,
    Sivaprasath

    Reply
  8. Sir,

    If my bean class has reference of other Class then how do I bind csv to my bean class, consider following example :

    public class Employee{
    private int empId;
    private int empName;
    private Address address;

    }

    public class Address {
    private String city;
    private String state;
    }

    And my Employee.csv file is like this :

    EmpId,EmpName,City,State
    101,xyz,abc,def

    Please clarify

    Reply
    • Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import. For CSV file, OpenCSV is better option.

      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.