Maven:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.11.1</version>
</dependency>
Gradle:
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.11.1'
public class Student {
private String name;
private int age;
private double score;
// ...
}
ObjectMapper objectMapper = new CsvMapper();
try {
File file = new File("students.csv");
List<Student> students = objectMapper.readValue(file, new TypeReference<List<Student>>() {});
for (Student student : students) {
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Score: " + student.getScore());
System.out.println("---");
}
} catch (IOException e) {
e.printStackTrace();
}
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 18, 92.5));
students.add(new Student("Bob", 19, 87.0));
students.add(new Student("Charlie", 20, 91.3));
try {
File file = new File("students.csv");
objectMapper.writeValue(file, students);
} catch (IOException e) {
e.printStackTrace();
}
CsvMapper csvMapper = new CsvMapper();