Java: Convert csv files with different columns into collections of objects

Question:

I have 4 csv files. Each of them stores different data and different column names (schemas). I need to parse these files and turn them into 4 Lists, which store objects of different types (depending on the file and its schema, for example Obj1, Obj2, Obj3. That is, I would like to create such a method in which I pass the class object that I want to create and the file itself, and this method will return me a sheet with these objects.

What's the best way to do this in Java, the most optimal way? Maybe there are any special libraries that allow you to do this?

Answer:

try opencsv , pay attention to com.opencsv.bean.HeaderColumnNameMappingStrategy or its subclasses for automatic mapping by column headers.

Scroll to Top
AllEscort