【问题标题】:Supercsv - unable to find method exceptionSupercsv - 无法找到方法异常
【发布时间】:2015-10-08 21:24:21
【问题描述】:

我有以下实现。

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)

【问题讨论】:

    标签: java parsing csv supercsv


    【解决方案1】:

    我不确定 SuperCsv,但 univocity-parsers 应该能够毫不费力地处理这个问题,更不用说解析输入的速度至少快 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 解析为 bean:

    // 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 许可)。

    【讨论】:

    • 我正在使用 univocity-parsers-1.5.6,并且在读取/解析文件时发现难以转义最后一行。你能帮我在读取/解析文件时如何忽略/转义最后一行吗?
    • 忽略最后解析的行怎么样?
    • 感谢您的回复。检查内容后,我从列表中删除了最后一行。但我一直在寻找更好的方法,比如从开始/结束跳过“n”行。或者,如果可以使用注释,并且可以将其数据类型从 char 更改为 string,以便它可以容纳字符和字符串。
    • 将'#'添加到行首,它将被忽略。您不能从末尾跳过行,因为只有在解析所有内容时才能知道结尾。
    • 其实..我无法控制写入文件
    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 2017-07-30
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多