0

Actually I have a class ArticleModele where I store the content of the columns of the .csv, but I don't know how to access to a particular instance of the class which corresponds to a particular row in the .csv. Here is my code:

 public static ArticleModele readWithCsvDozerBeanReader() throws Exception {   
     final CellProcessor[] processors = new CellProcessor[] { 

     new Optional(),
     new Optional(),
     new Optional()

     };

     ICsvDozerBeanReader beanReader = null;
     try {
     beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME),   CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);

     beanReader.getHeader(true); // ignore the header
     beanReader.configureBeanMapping(ArticleModele.class, FIELD_MAPPING);

     ArticleModele articleModele;
     while( (articleModele = beanReader.read(ArticleModele.class, processors)) != null )        {
     System.out.println(String.format(" %s", articleModele));}
     return articleModele;
     }
     finally {
     if( beanReader != null ) {
     beanReader.close();
      }
     }
    }
  }

And here is the class:

public class ArticleModele {
    public String titre;
    public String contenu;
    public String attachement; 

    public ArticleModele(){}

    public ArticleModele(String titre, String contenu, String attachement){
    this.titre=titre;
    this.contenu=contenu;
    this.attachement=attachement;

    }
    public String getTitre(){
    return titre;
    }
    public void  setTitre(String titre){
    this.titre=titre;
    }
   public String getContenu(){
   return contenu;
  
   }
    public void setContenu(String contenu){
    this.contenu=contenu;
   }
    
    public String getAttachement(){
    return attachement;
      
      }
    public void setAttachement(String attachement){
    this.attachement=attachement;    
       }
    public String toString() {
    return String.format("ArticleModele [titre=%s, content=%s, attachement=%s]",      titre, contenu, attachement);
   }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • instead of returning single ArticleModele , you can return a list and process that as per your needs. You can access whatever row instance you want by fetching it from that list. – user3487063 Aug 11 '14 at 14:02

2 Answers2

1

The code returns with last result, as it overwrites articleModele.

ArticleModele articleModele;
while( (articleModele = beanReader.read(ArticleModele.class, processors))
        != null)             {
    System.out.println(articleModele);
}
return articleModele;

So collect a list.

public static List<ArticleModele> readWithCsvDozerBeanReader() throws Exception { 
    List<ArticleModele> articleModele = new ArrayList<>();
    ArticleModele articleModele;
    while( (articleModele = beanReader.read(ArticleModele.class, processors))
         != null)             {
        System.out.println(articleModele);
        articleModeles.add(articleModele);
    }
    return articleModeles;

If this works you can get the ith article. And walk the articles:

for (ArticleModele articleModele : articleModeles) { ...
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

For example if you want to fetch a row by title, you can have something like:

public static Map<String, ArticleModele> readWithCsvDozerBeanReader() throws Exception {   
     final CellProcessor[] processors = new CellProcessor[] { 

     new Optional(),
     new Optional(),
     new Optional()

     };
Map<String, ArticleModele> map = new HashMap<String,ArticleModele>();
     ICsvDozerBeanReader beanReader = null;
     try {
     beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME),   CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);

     beanReader.getHeader(true); // ignore the header
     beanReader.configureBeanMapping(ArticleModele.class, FIELD_MAPPING);

     ArticleModele articleModele;
     while( (articleModele = beanReader.read(ArticleModele.class, processors)) != null )        {
     System.out.println(String.format(" %s", articleModele));}
     map.put(articleModele .getTitre(),articleModele);
     }
     finally {
     if( beanReader != null ) {
     beanReader.close();
      }
     }
    }

  }

and get whatever articleModele you want using:

map.get("titre");
user3487063
  • 3,672
  • 1
  • 17
  • 24