-1

I m using super csv's bean reader to read my csv files. It works fine if the POJO and columns match.

For eg My pojo has theses values.

//They have getters and setters.
//just to make it simple I have used public.
public myPojo
{
 public columnA;
 public columnB;
}

My super csv code looks like this.

        final String[] header = beanReader.getHeader(true);
        int amountOfColumns=beanReader.length();
        CellProcessor[] processor = new CellProcessor[amountOfColumns];

so if my csv files looked like this it works fine.

columnA,columnB

value1,value2

but if my csv files had an extra column, It fails with this exception check that the corresponding nameMapping element matches the field name in the bean. Can I avoid this exception and just ignore this column. Basically it says it could not find the corresponding setter.

unknownColumn,columnA,columnB

someValue,value1,value2

user1364861
  • 301
  • 1
  • 2
  • 16

1 Answers1

1

I used listReader instead of bean reader. got the column values to a map and I was able to do something like this.

protected MyPojo getBeanFromMap(MyPojo MyPojo, Class objClass,
                                    Map<String, String> rowMap)
        throws NoSuchFieldException, IllegalAccessException
{
    Field[] fields = objClass.getFields();
    for(Field f : fields){
        String fieldName = f.getName();
        String value = rowMap.get(fieldName);
        MyPojo.setField(fieldName, value);
    }
    return  MyPojo;
}

public void setField(String fieldName, Object value)
        throws NoSuchFieldException, IllegalAccessException
{
    Field field = getClass().getDeclaredField(fieldName);
    field.set(this, value);
}
user1364861
  • 301
  • 1
  • 2
  • 16