When working with CSV files in Java, handling raw string arrays can get messy. Instead of dealing with indexes like line[0]
, line[1]
, and so on, you can map CSV data directly to Java objects. This is where OpenCSV’s @CsvBindByName
annotation comes in handy. It allows you to bind CSV columns to object fields by using the column names in the header row.
In this tutorial, you’ll learn how to use @CsvBindByName
with OpenCSV to map CSV data into Java objects seamlessly. This approach improves readability, reduces errors, and makes your code much easier to maintain.

Why Use @CsvBindByName
?
Here are the key benefits:
- Readability – No more hardcoding column indexes.
- Flexibility – Works even if the column order changes in the CSV.
- Maintainability – Easier to update when new columns are added.
- Type Safety – Direct mapping to appropriate Java fields.
Step 1: Add OpenCSV Maven Dependency
Add the following to your pom.xml
:
1 2 3 4 5 6 7 8 |
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.9</version> </dependency> |
Step 2: Create a Java Class with Annotations
Suppose we have a CSV file (employees.csv
) like this:
1 2 3 4 5 6 7 |
Name,Age,Department Alice,24,Engineering Bob,30,HR Charlie,28,Finance |
We create a Java class with fields annotated using @CsvBindByName
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kscodes.opencsv; import com.opencsv.bean.CsvBindByName; public class Employee { @CsvBindByName(column = "Name") private String name; @CsvBindByName(column = "Age") private int age; @CsvBindByName(column = "Department") private String department; // Getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } } |
Step 3: Read CSV into Java Objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.kscodes.opencsv; import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBeanBuilder; import java.io.FileReader; import java.io.Reader; import java.util.List; public class CsvToObjectExample { public static void main(String[] args) { String filePath = "employees.csv"; try (Reader reader = new FileReader(filePath)) { CsvToBean<Employee> csvToBean = new CsvToBeanBuilder<Employee>(reader) .withType(Employee.class) .withIgnoreLeadingWhiteSpace(true) .build(); List<Employee> employees = csvToBean.parse(); for (Employee emp : employees) { System.out.println(emp.getName() + " | " + emp.getAge() + " | " + emp.getDepartment()); } } catch (Exception e) { e.printStackTrace(); } } } |
Output
1 2 3 4 5 6 |
Alice | 24 | Engineering Bob | 30 | HR Charlie | 28 | Finance |
Best Practices
- Always include headers in your CSV when using
@CsvBindByName
. - Use matching field names and column names to avoid mapping issues.
- For large files, consider streaming with OpenCSV to improve performance.
Conclusion
Using OpenCSV with @CsvBindByName
makes CSV parsing much more intuitive. Instead of manually handling arrays, you map data directly to Java objects, which improves code clarity and reduces bugs. This method is especially useful in enterprise applications where data changes frequently.
For further reading: