我有一个带有setter和getter的类文件

private double Amount;
private Date abcDate;

public double getAmount() {
    return Amount;
}
public void setAmount(double amount) {
    Amount = amount;
}
public Date getAbcDate() {
    return abcDate;
}
public void setAbcDate(Date abcDate) {
    this.abcDate = abcDate;
}


我有一个CSV文件

Amount, 1000
abcDate, 12/03/2018
PersonName, John
PersonLocation, Berlin


我想读取CSV文件并通过设置器实例化变量。我可以使用CSVReader,Univocity,openCSV等读取CSV文件。

如何将其与setter类进行比较并设置值?

最佳答案

如果您的问题仅在于匹配,则可以通过common-csv liberary像下面这样解决:

Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
    String amount = record.get("Amount");
    String abcDate = record.get("abcDate");

    MyDto dto = new MyDto() // use your class name here
    dto.setAmount(amount);
    dto.setAbcDate(abcDate);
    // continue to process object -> store to collection, etc
}

10-06 05:24