I just started with Java and have a lot of missing knowledge, but i need to write a simple class that will convert csv file using openCSV into a JavaBean. I have found some answers to similar quetions here, but none was able to help me. So far, a have come accross this bit of code:
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(YourOrderBean.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);
CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, yourReader);
which is located on openCSV FAQ site and also in another question here. The problem is I cannot find out specification how the object ColumnPositionMappingStrategy should look like and also what should be passed in strat.setType statement (YourOrder Bean.class). setColumnMapping method is also uncelar to me, but i belive i can figure that one out by myself when I know the rest...
Would anyone be kind enough to explain this bit of code more? The openCSV documentation is very brief for me, as i lack some basic knowlage of Java (coming from PHP, which has major differences)
Thanks in advance!
This is defining how to map the String[]
that is a row of a CSV to the properties of your JavaBean
.
Lets assume you have a class
like so:
public class JavaBeanExample {
private Integer id;
private String name;
private Integer orderNumber;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(Integer orderNumber) {
this.orderNumber = orderNumber;
}
}
The key points to note about this class are:
getXxxx
that return their value.setXxxx
that set their value.Now; the first method, setType
, takes the class of your bean. It uses this to create instances via reflection. In this case we would call:
strat.setType(JavaBeanExample.class);
Next, let us assume that we have a CSV in the following format
Name,Order Number, Order Id
Joe Bloggs, 77777, 00001
John Smith, 77778, 00002
So we need to map the first column to our name
property, the second to our orderNumber
property and the third to id
. We use the property names in the bean to tell OpenCSV which setter to use. OpenCSV then uses a PropertyDescriptor
to set the property via a correspondingly named setter.
In this case we would call
String[] columns = new String[] {"name", "orderNumber", "id"};
strat.setColumnMapping(columns);
Now this is all set up we can kick OpenCSV by calling
List list = csv.parse(strat, yourReader);
This will return a List
of JavaBeanExample
, one for each row in your file.
But this is somewhat unpleasant because we have to cast each and every item in the List
. This is due to the example being somewhat outdated. Here is an example making use of generics, this example in written in Java 7.
final ColumnPositionMappingStrategy<JavaBeanExample> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(JavaBeanExample.class);
strategy.setColumnMapping(new String[]{"name", "orderNumber", "id"});
final CsvToBean<JavaBeanExample> csvToBean = new CsvToBean<>();
final List<JavaBeanExample> beanExamples;
try (final Reader reader = new FileReader("myFile.csv")) {
beanExamples = csvToBean.parse(strategy, reader);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
The difference here is that we tell the ColumnPositionMappingStrategy
its generic type using the angle brackets. We also tell the CsvToBean
its generic type. This means that when we call parse a List<JavaBeanExample>
is returned; i.e. a List
knowing its generic type. Now we don't have to cast the individual elements in the List
.