java - 在java中使用SuperCsv读取tsv文件时出现异常

标签 java csv supercsv

我的 tsv 文件包含:

 tax_id GeneID Symbol

9606    1   A1BG

TsvReader.java

private static CellProcessor[] getProcessors() {

    final CellProcessor[] processors = new CellProcessor[] {

    new NotNull(), // tax_id
            new NotNull(), // GeneID
            new NotNull(), // Symbol


    };

    return processors;
}

private static void readCsvBeanReader() throws Exception {

    ICsvBeanReader beanReader = null;
    try {
        beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME),
                CsvPreference.TAB_PREFERENCE);

        // the header elements are used to map the values to the bean (names
        // must match)
        final String[] header = beanReader.getHeader(true);
        final CellProcessor[] processors = getProcessors();

        TsvEntities tsv_ent;
        while ((tsv_ent = beanReader.read(TsvEntities.class, header,
                processors)) != null) {

            System.out.println(String.format("tsv_ent=%s",tsv_ent));


        }

    } finally {
        if (beanReader != null) {
            beanReader.close();
        }
    }
}

TsvEntities.java

package com.tsvreader;

public class TsvEntities {

    private Integer tax_id;
    private Integer GeneID;
    private String Symbol;


    public TsvEntities() {
    }

    public TsvEntities(final Integer tax_id, final Integer GeneID, final String Symbol){

        this.tax_id = tax_id;
        this.GeneID= GeneID;
        this.Symbol=Symbol;

    }

    public Integer getTax_id() {
        return tax_id;
    }

    public void setTax_id(Integer tax_id) {
        this.tax_id = tax_id;
    }

    public Integer getGeneID() {
        return GeneID;
    }

    public void setGeneID(Integer geneID) {
        this.GeneID = geneID;
    }

    public String getSymbol() {
        return Symbol;
    }

    public void setSymbol(String symbol) {
        this.Symbol = symbol;
    }



    @Override
    public String toString() {

    return String
            .format("TsvEntities [tax_id=%s, GeneID=%s, Symbol=%s]",getTax_id(), getGeneID(), getSymbol());



}

异常:

Exception in thread "main" org.supercsv.exception.SuperCsvReflectionException: unable to find method setTax_id GeneID Symbol(java.lang.String) in class com.tsvreader.TsvEntities - 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:183)
    at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:95)
    at org.supercsv.io.CsvBeanReader.populateBean(CsvBeanReader.java:158)
    at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:207)
    at com.tsvreader.TsvReader.readCsvBeanReader(TsvReader.java:59)
    at com.tsvreader.TsvReader.main(TsvReader.java:17)

最佳答案

乍一看,我会说你的问题是 public void setTax_id(Integertax_id) 采用一个 Integer 参数,但 SuperCsv 希望它采用一个字符串。您应该提供一个采用 StringsetTax_id 方法。 setGeneID 也是如此。

编辑:

第一部分不正确。似乎您的文件头未正确解析。 SuperCsv 将整行解释为方法名称。检查第一行是否正确分隔。 (从你的问题来看,第一行似乎是用空格而不是制表符分隔的。)

关于java - 在java中使用SuperCsv读取tsv文件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17525444/

相关文章:

java - 在 CSV 文件中创建多个工作表

java - 下拉菜单未按预期工作

java - 通过 ItemListener 更改 ListView

python - 无法通过在 Python 中搜索 csv 文件获得结果

Mysql导入缩短csv字符串值

java - SuperCSV Joda 时间 LocalDate 单元处理器接受多种日期格式

java - superCSV java 库内存不足

java - tomcat使用哪个java版本

java - java编译器如何解析非导入的名称

php - 数组到 CSV 导出功能在 WordPress 插件中面临问题