-
Notifications
You must be signed in to change notification settings - Fork 1
Example: Write dynamic columns, fields (with Collection or Array)
lecogiteur edited this page Jun 2, 2013
·
1 revision
package my.package1;
@CsvType
public class MyCsvBean{
@CsvField(position=1)
private String name;
@CsvField(position=2)
private String address;
@CsvField(position=3)
private int numberOfPerson;
@CsvField(position=4)
@CsvFormat(type=TYPE_FORMAT.BOOLEAN, pattern="boolean")
private int[] numbers;
@CsvField(position=5, deleteIfNull=true)
private Collection<String> collections;
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public int getNumberOfPerson(){
return numberOfPerson;
}
public int[] getNumbers(){
return numbers;
}
public Collection<String> setCollections(){
return collections;
}
public void setAddress(String address){
this.address = address;
}
public void setName(String name){
this.name = name;
}
public void setNumberOfPerson(int numberOfPerson){
this.numberOfPerson = numberOfPerson;
}
public void setNumbers(int[] numbers){
this.numbers = numbers;
}
public void setCollections(Collection<String> collections){
this.collections = collections;
}
}
package my.package1;
public class MyMain{
public static MyCsvBean getBean(){
final MyCsvBean bean = new MyCsvBean();
bean.setName("my name");
bean.setAddress("my address");
bean.setNumberOfPerson(10);
bean.setNumbers({1, 0, 1, 1});
bean.setCollections(Arrays.asList({"A", "B", null, "D"}));
return bean;
}
public static void main (String[] args){
final FactoryCsvWriter factory = new FactoryCsvWriter("my.package1");
final CsvWriter writer = factory.createCsvWriter(MyCsvBean.class, "/tmp/myfile.csv");
MyCsvBean bean = getBean();
writer.write(bean);
bean.setNumbers({0, 1});
bean.setCollections(Arrays.asList({"N", "O"}));
writer.write(bean);
write.close();
}
}
Content of File: /tmp/myfile.csv
my name,my address,10,true,false,true,true,A,B,D
my name,my address,10,false,true,N,O