How to convert my bean MyClassCsv
to a CSV file with the name of an optional column? The column 'title' must be contained or not in the CSV file.
The bean:
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MyClassCsv implements Serializable {
private static final long serialVersionUID = -6476622625063619084L;
@CsvBindByName(column = "id")
private Integer id;
@CsvBindByName(column = "name")
private String name;
@CsvBindByName(column = "title")
private String title;
...
}
Write to CSV:
final Writer writer = new FileWriter(fileCsv.getPath());
try {
final HeaderColumnNameMappingStrategy<HoteIncbCsv> strategy = new HeaderColumnNameMappingStrategy<>();
strategy.setType(MyClassCsv.class);
// **** optional mapped 'title' ***** //
if (!useTitle) {
// ?????????
}
final StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).withMappingStrategy(strategy).withSeparator(';').withApplyQuotesToAll(false).build();
// Ecriture dans le fichier CSV de sortie
beanToCsv.write(myClassCsv);
} finally {
writer.close();
}