Examples of the use of Super CSV DOZER extensions in the Java class library
Super CSV Dozer is a Java class library for conversion between CSV files and Java objects.This extension module combines the functions of the Super CSV and Dozer framework, which can easily process the data in the CSV file and map it to the Java object.
The following is an example that shows how to use the Super CSV Dozer extension to read the CSV file and convert it to the Java object.
First, we need to add Super CSV Dozer to the Maven project:
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv-dozer</artifactId>
<version>2.4.0</version>
</dependency>
Suppose we have a Java class called "Employee", which contains the name, age and salary information of the employee.
public class Employee {
private String name;
private int age;
private double salary;
// The constructor and the getter/setter method are omitted
}
Next, we create a CSV file, each of which contains a employee's information, separated by a comma between the fields.
name,age,salary
John Smith,30,50000
Alice Johnson,25,40000
In the code, we first need to define the configuration file of Dozer, and specify the mapping relationship between the column name of the CSV and the attributes of the Java object.Create a file called "Dozer-MAPPINGS.XML" and add the following content to the file:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mappings"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mappings
http://dozermapper.github.io/schema/bean-mappings.xsd">
<mapping date-format="dd/MM/yyyy" wildcard="true">
<class-a>com.example.Employee</class-a>
<class-b>java.util.Map</class-b>
<field-exclude type="one-way">
<a>name</a>
<b>*</b>
</field-exclude>
</mapping>
</mappings>
In this configuration file, we map the CSV's "Age" and "SALARY" fields to the corresponding properties of the Employee class, and the "name" field is excluded from the mapping.Such configurations can be adjusted according to actual needs.
Next, we can write the Java code to read the CSV file and convert it.Assuming that the CSV file is called "Employees.csv", the code is shown below:
import org.supercsv.dozer.CsvDozerBeanReader;
import java.io.FileReader;
import java.util.List;
public class CSVtoJavaObjectConverter {
public static void main(String[] args) throws Exception {
try (CsvDozerBeanReader beanReader = new CsvDozerBeanReader(new FileReader("employees.csv"), CsvPreference.STANDARD_PREFERENCE, new DozerBeanMapperBuilder().withMappingFiles("dozer-mappings.xml").build())) {
final String[] header = beanReader.getHeader(true);
final String[] fieldMapping = {"name", "age", "salary"};
final CellProcessor[] processors = new CellProcessor[] {new NotNull(), new NotNull(), new Optional()};
List<Employee> employees = new ArrayList<>();
Employee employee;
while ((employee = beanReader.read(Employee.class, fieldMapping, processors)) != null) {
employees.add(employee);
}
for (Employee emp : employees) {
System.out.println(emp.getName() + ", " + emp.getAge() + ", " + emp.getSalary());
}
}
}
}
In this code, we first created a CSVDOZERBEANREADER object, using Filereader to read the "Employees.csv" file, and load the previously created DOZER configuration file.We then define the header, field mapping relationship, and processors of the CSV file.
In the cycle, use the BeanReader.read method to convert each line of data in the CSV file into an Employee object and add it to a list.
Finally, we traversed the name, age, and salary information of each employee object.
In summary, the Super CSV Dozer extension provides a simple and powerful way to handle the CSV file and convert it into a Java object.By configured mapping relationships and processors, we can flexibly convert data according to our own needs.