java - SUPER CSV 将 bean 写入 CSV

标签 java csv javabeans supercsv

这是我的课,

public class FreebasePeopleResults {

public String intendedSearch;
public String weight;
public Double heightMeters;
public Integer age;
public String type;
public String parents;
public String profession;
public String alias;
public String children;
public String siblings;
public String spouse;
public String degree;
public String institution;
public String wikipediaId;
public String guid;
public String id;
public String gender;
public String name;
public String ethnicity;
public String articleText;
public String dob;

public String getWeight() {
    return weight;
}
public void setWeight(String weight) {
    this.weight = weight;
}
public Double getHeightMeters() {
    return heightMeters;
}
public void setHeightMeters(Double heightMeters) {
    this.heightMeters = heightMeters;
}
public String getParents() {
    return parents;
}
public void setParents(String parents) {
    this.parents = parents;
}
public Integer getAge() {
    return age;
}
public void setAge(Integer age) {
    this.age = age;
}       
public String getProfession() {
    return profession;
}
public void setProfession(String profession) {
    this.profession = profession;
}
public String getAlias() {
    return alias;
}
public void setAlias(String alias) {
    this.alias = alias;
}
public String getChildren() {
    return children;
}
public void setChildren(String children) {
    this.children = children;
}
public String getSpouse() {
    return spouse;
}
public void setSpouse(String spouse) {
    this.spouse = spouse;
}
public String getDegree() {
    return degree;
}
public void setDegree(String degree) {
    this.degree = degree;
}
public String getInstitution() {
    return institution;
}
public void setInstitution(String institution) {
    this.institution = institution;
}
public String getWikipediaId() {
    return wikipediaId;
}
public void setWikipediaId(String wikipediaId) {
    this.wikipediaId = wikipediaId;
}
public String getGuid() {
    return guid;
}
public void setGuid(String guid) {
    this.guid = guid;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEthnicity() {
    return ethnicity;
}
public void setEthnicity(String ethnicity) {
    this.ethnicity = ethnicity;
}
public String getArticleText() {
    return articleText;
}
public void setArticleText(String articleText) {
    this.articleText = articleText;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String getSiblings() {
    return siblings;
}
public void setSiblings(String siblings) {
    this.siblings = siblings;
}
public String getIntendedSearch() {
    return intendedSearch;
}
public void setIntendedSearch(String intendedSearch) {
    this.intendedSearch = intendedSearch;
}

这是我的 CSV 编写器方法

 import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.supercsv.io.CsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

public class CSVUtils {

    public static void writeCSVFromList(ArrayList<FreebasePeopleResults> people, boolean writeHeader) throws IOException{

        //String[] header = new String []{"title","acronym","globalId","interfaceId","developer","description","publisher","genre","subGenre","platform","esrb","reviewScore","releaseDate","price","cheatArticleId"};
        FileWriter file = new FileWriter("/brian/brian/Documents/people-freebase.csv", true);

        // write the partial data
        CsvBeanWriter writer = new CsvBeanWriter(file, CsvPreference.EXCEL_PREFERENCE);

        for(FreebasePeopleResults person:people){
            writer.write(person);
        }
        writer.close();
        // show output
    }           
}

我一直收到输出错误。这是错误: 没有要为第 2 行上下文编写的内容:行:2 列:0 原始行: 空

现在,我知道它现在完全是空的,所以我很困惑。

最佳答案

所以已经有一段时间了,您可能已经离开了这里,但是...

问题实际上是您没有向 write() 方法提供 header ,即它应该是

writer.write(person, header);

不幸的是,API 在 write() 方法的签名中使用了 var-args 表示法,这有点误导,因为它允许传递 null in. javadoc 清楚地指出你不应该这样做,但是在实现中没有空检查:因此你得到了异常。

/**
 * Write an object
 * 
 * @param source
 *            at object (bean instance) whose values to extract
 * @param nameMapping
 *            defines the fields of the class that must be written. 
 *            null values are not allowed
 * @since 1.0
 */
public void write(Object source, String... nameMapping) throws IOException,
    SuperCSVReflectionException;

Super CSV 2.0.0-beta-1现在出来了。它在 write() 方法中保留了 var-args,但如果您提供 null,则会很快失败,因此当您收到带有以下内容的 NullPointerException 时,您会确切地知道出了什么问题:

the nameMapping array can't be null as it's used to map from fields to columns

它还包括许多错误修复和新功能(包括 Maven 支持和用于映射嵌套属性和数组/集合的新 Dozer 扩展)。

关于java - SUPER CSV 将 bean 写入 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2806870/

相关文章:

java - Play Framework 停留在同一页面然后更改语言

python - Pandas read_csv() 损失数字精度

java - 无法正确获取 spring-security.xml

java - Gradle +网 bean

java - replaceAll 的问题(我想删除所有出现的 [*])

mysql - 返回mysql查询的列名

python - pd.read_csv 中的字符串行索引导致错误 "The label [1] is not in the [index]"

java - 带参数的 Spring Bean 和 @Autowired 构造函数

xml - 使用 <import> 时出现 Spring/Eclipse 'referenced bean not found' 警告?

java - 如何在 JBoss 中预览服务器上的文件