3

I'm using OpenCSV 3.8 to write Data into CSV files.

I have multiple class, Train, Tickets... So I don't want to write the same code for each class.

I have done this (I removed some lines that are not important):

 public <T extends ExportDTO> void exportByTable(List<T> exportList) {
    Writer csvWriter = new FileWriter(csvPath);
    StatefulBeanToCsvBuilder<T> builder = new StatefulBeanToCsvBuilder<T>(csvWriter);

    StatefulBeanToCsv<T> sbc = builder
            .withSeparator(CSV_SEPARATOR)
            .build();

    sbc.write(exportList);

Every class has ExportDTO as a superclass, in ExportDTO there is nothing, but in Ticket or Train I have attributes with annotations (opencsv).

But this code write an empty CSV because it take ExportDTO class that has no attribute to write the CSV. (I call the method with a List<Train> object)

Am I doing something wrong ? or is it impossible to do a generic method for this ?

Alkalyne
  • 153
  • 6
  • It sounds like it should be possible to create a generic solution for your case. Please, could you provide some more information? Maybe an example of your expectations on how such API could look like? Or on which class (ExportDTO or other?) should the saving method be invoked? – Danon Jun 27 '18 at 12:13
  • 1
    So... it work now, the problem wasn't the generic solution (I though the file was empty because it was Using ExportDTO class to export instead of TrainExport) But it was because of a missing dependency in opencsv jar (I needed to compile it myself to get it work with Java 1.6). – Alkalyne Jun 27 '18 at 13:06

0 Answers0