Java - how to read CSV file into java object with list of numbers using Jackson CSV library
In this post we will read CSV file into java pojo object with list of numbers using Jackson CSV processor.
This example contains 2 lists. First list contains Integers, second list contains Doubles.
Lists of Integer and Double in UserWithListsDto class:
- List<Integer> numbers
- List<Double> floatingPointNumbers
To use Jackson CSV library we need to add jackson-dataformat-csv dependency to pom.xml.
xxxxxxxxxx
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.9.9</version>
</dependency>
Below we have all classes we need to read csv file.
Ensure to create csv file on your disc and set correct path in UserWithListsCsvReader class.
To run this example copy to your project those 3 files:
- UserWithListsCsvReader
- UserWithListsDto
- users_with_numbers_list.csv
How does it work?
- UserWithListsDto has constructor with 'List<String> csvRow'.
- In UserWithListsCsvReader we read all lines from csv file and invoke constructor of UserWithListsDto with list of all columns of single line of our csv line.
I didn't find any better solution with this library, so if anyone know better way to do it, post it in comment.
2.1 Logic - UserWithListsCsvReader
Logic which reads CSV file into java UserWithListsDto pojo object with Jackson CSV library.
Usage of classes:
CsvMapper + CsvParser from jackson CSV library
MappingIterator from jackson databind library
xxxxxxxxxx
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class UserWithListsCsvReader {
public static void main(String[] args) throws IOException {
// set correct path to csv file on your disc
File csvFile = new File("C:\\csv_tests\\users_with_numbers_list.csv");
CsvMapper csvMapper = new CsvMapper();
csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
MappingIterator<List<String>> rows = csvMapper.readerFor(List.class)
.readValues(csvFile);
List<List<String>> lists = rows.readAll();
List<UserWithListsDto> users = lists.stream().skip(1) // skip header
.map(UserWithListsDto::new)
.collect(Collectors.toList());
users.forEach(System.out::println);
}
}
Output:
xxxxxxxxxx
UserWithListsDto{id=1, name='Ann', age=30, numbers=[100, 200, 300], floatingPointNumbers=[4.14, 2.134]}
UserWithListsDto{id=2, name='Seth', age=25, numbers=[333, 444, 555], floatingPointNumbers=[92.123, 123.123]}
UserWithListsDto{id=3, name='Tom', age=27, numbers=[99, 88, 77], floatingPointNumbers=[3.14, 2.71]}
2.2 User Pojo object - UserWithListsDto
xxxxxxxxxx
import java.util.ArrayList;
import java.util.List;
public class UserWithListsDto {
private Long id;
private String name;
private Integer age;
private List<Integer> numbers;
private List<Double> floatingPointNumbers;
public UserWithListsDto() {
}
public UserWithListsDto(List<String> csvRow) {
int csvColumnIndex = 0;
this.id = Long.valueOf(csvRow.get(csvColumnIndex++));
this.name = csvRow.get(csvColumnIndex++);
this.age = Integer.valueOf(csvRow.get(csvColumnIndex++));
// read 3 numbers
this.numbers = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Integer num = Integer.valueOf(csvRow.get(csvColumnIndex++));
numbers.add(num);
}
// read 2 double numbers
this.floatingPointNumbers = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Double num = Double.valueOf(csvRow.get(csvColumnIndex++));
floatingPointNumbers.add(num);
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Integer> getNumbers() {
return numbers;
}
public void setNumbers(List<Integer> numbers) {
this.numbers = numbers;
}
public List<Double> getFloatingPointNumbers() {
return floatingPointNumbers;
}
public void setFloatingPointNumbers(List<Double> floatingPointNumbers) {
this.floatingPointNumbers = floatingPointNumbers;
}
public String toString() {
return "UserWithListsDto{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", numbers=" + numbers +
", floatingPointNumbers=" + floatingPointNumbers +
'}';
}
}
2.3 User with numbers list - CSV file
Save this file on disc as 'users_with_numbers_list.csv' and change location in UserWithListsCsvReader class.
xxxxxxxxxx
id,name,age,numbers,floatingPointNumbers
1,Ann,30,100,200,300,4.14,2.134
2,Seth,25,333,444,555,92.123,123.123
3,Tom,27,99,88,77,3.14,2.71
2.4 Csv reader util
UserWithListsCsvReaderUtil allows us to read csv from line or convert csv in String to java pojo objects.
xxxxxxxxxx
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class UserWithListsCsvReaderUtil {
public static List<UserWithListsDto> readCsvFromFile(File csvFile) {
try {
CsvMapper csvMapper = new CsvMapper();
csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
MappingIterator<List<String>> rows = csvMapper.readerFor(List.class)
.readValues(csvFile);
List<List<String>> lists = rows.readAll();
return lists.stream().skip(1)
.map(UserWithListsDto::new)
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// input - csv as 1 string - each line separated by new line '\n'
public static List<UserWithListsDto> readCsvFromString(String csvString) {
String[] csvLines = csvString.split("\n"); // 5 lines
return Arrays.stream(csvLines)
.skip(1) // skip header
.map(csvLine -> Arrays.stream(csvLine.split(","))
.collect(Collectors.toList()))
.map(UserWithListsDto::new)
.collect(Collectors.toList());
}
public static void main(String[] args) throws IOException {
// set correct path to csv file on your disc
File csvFile = new File("C:\\csv_tests\\users_with_numbers_list.csv");
System.out.println("Example 1 - from file");
List<UserWithListsDto> userWithListsDtos = readCsvFromFile(csvFile);
userWithListsDtos.forEach(System.out::println);
System.out.println();
System.out.println("Example 2 - from csv line as String");
// we can get csv via linux command line eg cat or just read all lines
List<String> allLines = Files.readAllLines(csvFile.toPath());
String csvInString = String.join("\n", allLines);
List<UserWithListsDto> userWithListsDtos1 = readCsvFromString(csvInString);
userWithListsDtos1.forEach(System.out::println);
}
}
Output:
xxxxxxxxxx
Example 1 - from file
UserWithListsDto{id=1, name='Ann', age=30, numbers=[100, 200, 300], floatingPointNumbers=[4.14, 2.134]}
UserWithListsDto{id=2, name='Seth', age=25, numbers=[333, 444, 555], floatingPointNumbers=[92.123, 123.123]}
UserWithListsDto{id=3, name='Tom', age=27, numbers=[99, 88, 77], floatingPointNumbers=[3.14, 2.71]}
Example 2 - from csv line as String
UserWithListsDto{id=1, name='Ann', age=30, numbers=[100, 200, 300], floatingPointNumbers=[4.14, 2.134]}
UserWithListsDto{id=2, name='Seth', age=25, numbers=[333, 444, 555], floatingPointNumbers=[92.123, 123.123]}
UserWithListsDto{id=3, name='Tom', age=27, numbers=[99, 88, 77], floatingPointNumbers=[3.14, 2.71]}