Is it possible to count the number of columns in a CSV file using BeanListProcessor? My goal is to verify that the number of headers is equal to the number of values in the CSV file.
I have tried this solution: what is the fastest way to get dimensions of a csv file in java
But this only applies to a RowProcessor, and I'm using the BeanListProcessor;
BeanListProcessor<SomeObject> rowProcessor = new BeanListProcessor<SomeObject>(SomeObject.class);
I tried overriding the method of RowProcessor in BeanProcess but it is declared final.
Is there any other way of validating CSV files with univocity?
You can always wrap row processors to implement any custom logic to manage how the input should be handled:
public class MyRowProcessor<T> implements RowProcessor {
private final BeanListProcessor<T> wrappedProcessor;
public MyRowProcessor(Class<T> beanType) {
wrappedProcessor = new BeanListProcessor<T>(beanType);
}
@Override
public void processStarted(ParsingContext context) {
wrappedProcessor.processStarted(context);
}
@Override
public void rowProcessed(String[] row, ParsingContext context) {
//validate the input row here
if(row.length != context.headers().length){
return; // Example: I'm skipping the row if the number of columns doesn't match number of headers
}
//send the row to the wrapped processor (or skip, or do anything you want)
wrappedProcessor.rowProcessed(row, context);
}
@Override
public void processEnded(ParsingContext context) {
wrappedProcessor.processEnded(context);
}
public List<T> getBeans() {
return wrappedProcessor.getBeans();
}
}
All you have to do now is to use your custom implementation:
MyRowProcessor<SomeObject> processor = new MyRowProcessor<SomeObject>(SomeObject.class);
settings.setRowProcessor(processor);
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
parser.parse(new File("/path/to/file"));
List<TestBean> beans = processor.getBeans();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With