title: Best design for Java Parsing several CSV files with different headers and model objects
body: <p>I have several CSV files that I will need to Parse. and use later for Insert in MYSQL. I have already written one parser but I want to avoid as much as I can code duplication</p>
  
  <p>I have already in mind that I should use an abstract class, or maybe a Factory but I can't really put a finger on the best way to design this.</p>
  
  <p>So this is my parser : </p>
  
  <pre><code>public class LocusAliasCsvParser {
  
  private static final String[] FILE_HEADER_MAPPING = {"id", "locusID", "organismid", "variable", "alias"};
  
  private static final String ID = "id";
  private static final String LOCUS_ID = "locusID";
  private static final String ORGANISM_ID = "organismid";
  private static final String VARIABLE = "variable";
  private static final String ALIAS = "alias";
  
  
  public static List&lt;AliasLocus&gt; readCsvFile(String fileName) {
  
      FileReader fileReader = null;
      CSVParser csvFileParser = null;
  
      CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);
      List&lt;AliasLocus&gt; aliases = new ArrayList();
  
      try {
          fileReader = new FileReader(fileName);
          csvFileParser = new CSVParser(fileReader, csvFileFormat);
  
          //Get a list of CSV file records
          List csvRecords = csvFileParser.getRecords();
  
          //Read the CSV file. Header is ignored (i == 1)
          for (int i = 1; i &lt; csvRecords.size(); i++) {
              CSVRecord record = (CSVRecord) csvRecords.get(i);
  
              AliasLocus aliasLocus = new AliasLocus(Integer.parseInt(record.get(ID)),
                      record.get(LOCUS_ID),
                      record.get(ORGANISM_ID),
                      record.get(VARIABLE),
                      record.get(ALIAS));
  
              aliases.add(aliasLocus);
          }
  
      } catch (Exception e) {
          System.out.println("Error in CsvFileReader !!!");
          e.printStackTrace();
      } finally {
          try {
              fileReader.close();
              csvFileParser.close();
          } catch (IOException e) {
              System.out.println("Error while closing fileReader/csvFileParser !!!");
              e.printStackTrace();
          }
      }
  
      return aliases;
  }
  </code></pre>
  
  <p>The things that change each time are :</p>
  
  <pre><code>public class LocusAliasCsvParser {
  
  private static final String[] FILE_HEADER_MAPPING = {"id", "locusID", "organismid", "variable", "alias"};
  
  private static final String ID = "id";
  private static final String LOCUS_ID = "locusID";
  private static final String ORGANISM_ID = "organismid";
  private static final String VARIABLE = "variable";
  private static final String ALIAS = "alias";
  </code></pre>
  
  <p>and :</p>
  
  <pre><code> public static List&lt;AliasLocus&gt; readCsvFile(String fileName) {
  
   AliasLocus aliasLocus = new AliasLocus(Integer.parseInt(record.get(ID)),
                      record.get(LOCUS_ID),
                      record.get(ORGANISM_ID),
                      record.get(VARIABLE),
                      record.get(ALIAS));
  </code></pre>
  
  <p>does anyone can suggest the best design patters or structure to use to have the least code duplication?</p>
  
  <p>thanks</p>