Mapping CSV to Java Beans Using OpenCSV Library
When we read data from CSV files, we sometimes need to map the data directly to Java objects.
The OpenCSV library provides the method to help us in such mapping and make the mapping process easy as mentioned below.
- First, create a
HashMap
with mapping between the CSV column names and Java object property names. - Next, create
HeaderColumnNameTranslateMappingStrategy
object by passing the mapping hashmap tosetColumnMapping()
method - Next, create the
CSVReader
andCsvToBean
objects. - Finally, call the
parse()
method on theCsvToBean
object.
Sample Data
Let's assume the below data in the CSV file, with the default separator.
ID,Name,Class,Marks
1,Arun,10,520
2,Sravan,10,630
3,Kiran,10,580
And, the corresponding Java Class looks as below.
public class Student {
private String id;
private String name;
private String standard;
private String marks;
}
1. Mapping CSV Columns & Java Object Properties
We have to build the mapping as shown below.
Map mapping = new HashMap();
mapping.put("ID", "id");
mapping.put("Name", "name");
mapping.put("Class", "standard");
mapping.put("Marks", "marks");
2. Creating Header Column Translation Strategy
Define the header column mapping strategy as shown below, which maps the CSV header columns to Java Bean Object properties.
HeaderColumnNameTranslateMappingStrategy strategy = new HeaderColumnNameTranslateMappingStrategy();
strategy.setType(Student.class);
strategy.setColumnMapping(mapping);
3. Extracting CSV Data into Java Objects
Define the CSVReader and CsvToBean objects as shown below.
String csvFilename = "data.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
CsvToBean csv = new CsvToBean();
List list = csv.parse(strategy, csvReader);
Complete Implementation
Here is the complete implementation.
import java.io.*;
import java.util.*;
import com.opencsv.CSVReader;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
public class CsvToBeanConversion {
public static void main(String[] args) {
// Mapping CSV columns to Bean properties.
Map<String, String> mapping = new HashMap<String, String>();
mapping.put("ID", "id");
mapping.put("Name", "name");
mapping.put("Class", "standard");
mapping.put("Marks", "marks");
// Defining CSV file header column translation mapping strategy
HeaderColumnNameTranslateMappingStrategy<Student> strategy = new HeaderColumnNameTranslateMappingStrategy<Student>();
strategy.setType(Student.class);
strategy.setColumnMapping(mapping);
// Defining CSVReader object
CSVReader csvReader = null;
try {
csvReader = new CSVReader(new FileReader("D:\\files\\sample.csv"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Defining CsvToBean object
CsvToBean csvToBean = new CsvToBean();
// Calling parse() method of CsvToBean object
List<Student> list = csvToBean.parse(strategy, csvReader);
// print details of Bean object
for (Student e : list) {
System.out.println(e);
}
}
}
Conclusion
Now, we know how to map CSV data to Java Beans while reading data from a CSV file.
Related Links
- Java - Writing data to a CSV file
- Java - Reading data from a CSV file
- Java - Mapping data from a CSV file to Java beans
- Java - Mapping data from Java beans to a CSV file