用于从CSV文件创建对象的Java API

Sim*_*eon 25 java csv

我正在寻找一个允许我将.csv内容映射到对象的库.

就像是:

public class Person {

    private String name;
    private int age;

    @CsvField("name")
    public String getName() {
        return this.name;
    }

    @CsvField("age")
    public int getAge() {
        return this.age;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后说出类似的话:

final Person filledWithDataFromCsv = csvApi.load(csvFilepath, Person.class);
Run Code Online (Sandbox Code Playgroud)

来自给定的CSV:

#name, age
tom, 11
jim, 32
Run Code Online (Sandbox Code Playgroud)

有没有人知道这样的API,或者做类似的事情.我不希望它使用注释作为必须,我只是希望能够使用一行代码和预定义的类加载文件.

Vin*_*lds 22

JSefa允许您注释可在序列化和反序列化过程中使用的Java类.本教程演示了如何使用CsvIOFactory类.

(从教程中)注释bean就像指定值列表中项目的位置一样简单,如有必要,您需要指定转换格式:

@CsvDataType()
public class Person {
    @CsvField(pos = 1)
    String name;

    @CsvField(pos = 2, format = "dd.MM.yyyy")
    Date   birthDate;
}
Run Code Online (Sandbox Code Playgroud)

  • @VineetReynolds我们可以在没有位置的情况下使用它,我的意思是在columnName的帮助下? (2认同)

Jer*_*kes 9

uniVocity解析器不会出错.它支持各种强大的操作,并且比任何其他Java解析器快得多.

这是一个有一些例子的类:

class TestBean {

    // if the value parsed in the quantity column is "?" or "-", it will be replaced by null.
    @NullString(nulls = { "?", "-" })
    // if a value resolves to null, it will be converted to the String "0".
    @Parsed(defaultNullRead = "0")
    private Integer quantity;   // The attribute type defines which conversion will be executed when processing the value.

    @Trim
    @LowerCase
    // the value for the comments attribute is in the column at index 4 (0 is the first column, so this means fifth column in the file)
    @Parsed(index = 4)
    private String comments;

    // you can also explicitly give the name of a column in the file.
    @Parsed(field = "amount")
    private BigDecimal amount;

    @Trim
    @LowerCase
    // values "no", "n" and "null" will be converted to false; values "yes" and "y" will be converted to true
    @BooleanString(falseStrings = { "no", "n", "null" }, trueStrings = { "yes", "y" })
    @Parsed
    private Boolean pending;
}
Run Code Online (Sandbox Code Playgroud)

以下是如何获取列表 TestBean

BeanListProcessor<TestBean> rowProcessor = new BeanListProcessor<TestBean>(TestBean.class);

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);

CsvParser parser = new CsvParser(parserSettings);
parser.parse(getReader("/examples/bean_test.csv"));

List<TestBean> beans = rowProcessor.getBeans();
Run Code Online (Sandbox Code Playgroud)

披露:我是这个图书馆的作者.它是开源和免费的(Apache V2.0许可证).


Tho*_*lut 7

我更喜欢opencsv,它非常简单,非常干净.

http://opencsv.sourceforge.net/

例如阅读:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
Run Code Online (Sandbox Code Playgroud)

  • 您的示例未映射到带有注释的 java bean。 (2认同)
  • + 第一,opencsv 截至 2020 年已更新 (2认同)