Supercsv - не удалось найти исключение метода

У меня есть реализация ниже.

csvReader = new CsvBeanReader(new InputStreamReader(stream), CsvPreference.STANDARD_PREFERENCE);
lastReadIdentity =  (T) csvReader.read(Packages.class, Packages.COLS);

В моем Packages.class

Я установил мою переменную unitcount.

public String getUnitCount() {
    return unitCount;
}
public void setUnitCount(String unitCount) {
    this.unitCount = unitCount;
}

Это прекрасно работает, когда оно берется как строка, но когда оно берется как целое число, оно выдает исключение ниже. Пожалуйста помоги

private int unitCount;
public int getUnitCount() {
    return unitCount;
}
public void setUnitCount(int unitCount) {
    this.unitCount = unitCount;
}

Исключение:

org.supercsv.exception.SuperCsvReflectionException: unable to find method setUnitCount(java.lang.String) in class com.directv.sms.data.SubscriberPackages - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field
context=null
at org.supercsv.util.ReflectionUtils.findSetter(ReflectionUtils.java:139)
at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:95)

1 ответ

Я не уверен насчет SuperCsv, но анализаторы univocity должны быть в состоянии справиться с этим без проблем, не говоря уже о том, что он как минимум в 3 раза быстрее анализирует ваши входные данные.

Просто аннотируйте свой класс:

public class SubscriberPackages {

    @Parsed(defaultNullRead = "0") // if the file contains nulls, then they will be converted to 0.
    private int unitCount;   // The attribute name will be matched against the column header in the file automatically.
}

Чтобы разобрать CSV в бобах:

// BeanListProcessor converts each parsed row to an instance of a given class, then stores each instance into a list.
BeanListProcessor<SubscriberPackages> rowProcessor = new BeanListProcessor<SubscriberPackages>(SubscriberPackages.class);

CsvParserSettings parserSettings = new CsvParserSettings(); //many options here, check the tutorial.
parserSettings.setRowProcessor(rowProcessor); //uses the bean processor to handle your input rows
parserSettings.setHeaderExtractionEnabled(true); // extracts header names from the input file.

CsvParser parser = new CsvParser(parserSettings); //creates a parser with your settings.
parser.parse(new FileReader(new File("/path/to/file.csv"))); //all rows parsed here go straight to the bean processor

// The BeanListProcessor provides a list of objects extracted from the input.
List<SubscriberPackages> beans = rowProcessor.getBeans();

Раскрытие: я являюсь автором этой библиотеки. Это с открытым исходным кодом и бесплатно (лицензия Apache V2.0).

Другие вопросы по тегам