1

If I have a class with non-primitive public members and I want to populate them from a CSV file with OpenCSV, how can I do this? I notice that OpenCSV has some protected members relating to PropertyDescriptors

So let's say I have a Person class that has an Address member, and my CSV file contains the detailsd for each person including their address..

Person{

private String name;

private Address al

public void setAddress(Address a){..}
public void setName(String name){..}

}

Addess{

private String line1;
private String line2;
private String postCode;

.
.
.

}

CSV file:

NAME        | ADDR1     | ADDR2     | PCODE ...
John Smith  |Some place | Some town | NW234

Thanks,

  • A
stacker
  • 68,052
  • 28
  • 140
  • 210
Aodh
  • 11
  • 1
  • 3
  • 2
    The example "CSV" file you posted **is not CSV**. CSV stands for Comma Separated Value; it looks like you're using pipes (`|`) instead of commas. – Matt Ball Nov 12 '10 at 14:20
  • 1
    CSV-files have used other separators than comma (,) for a while to make it more fleksible. So the C in CSV could be called CharacterSeparatedValues instead. – kometen Jan 31 '19 at 15:12

2 Answers2

5

I'm sure you've long since moved on, but I've run into the same situation and there are two ways to handle it. You can either override CsvToBean.convertValue or CsvToBean.getPropertyEditor.

The classier way is probably to override getPropertyEditor and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:

CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

    @Override
    protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

        if (prop.getName().equals("myWhatever")) {
            // return an custom object based on the incoming value
            return new MyWhatever((String)value);
        }

        return super.convertValue(value, prop);
    }
};

This is working fine for me with OpenCSV 2.3. Good luck!

Nathan Beach
  • 2,497
  • 2
  • 24
  • 25
5

Have you checked out the following FAQ question? It sounds to me like that's what you want to do. I could be mistaken though.

Is there a way to bind my CSV file to a list of Javabeans?

Yes there is.

Kyle Miller added a new set of classes to allow you to bind a CSV file to a list of JavaBeans based on column name, column position, or a custom mapping strategy. You can find the new classes in the au.com.bytecode.opencsv.bean package. Here's how you can map to a java bean based on the field positions in your CSV file:

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy(); 
strat.setType(YourOrderBean.class); 
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean 
strat.setColumnMapping(columns); 
CsvToBean csv = new CsvToBean(); 
List list = csv.parse(strat, yourReader);
extraneon
  • 23,575
  • 2
  • 47
  • 51