我是OpenCSV的新用户,我正在尝试解析在REST服务中收到的CSV文件。
这是我的代码:
CSVReader csvReader = new CSVReader(new InputStreamReader(csv), ';', CSVParser.DEFAULT_QUOTE_CHARACTER);
HeaderColumnNameTranslateMappingStrategy<TesteBean> strategy = new HeaderColumnNameTranslateMappingStrategy<>();
Map<String, String> columnMap = new HashMap<>();
columnMap.put("NOME", "nome");
columnMap.put("VALOR", "valor");
strategy.setColumnMapping(columnMap);
strategy.setType(TesteBean.class);
CsvToBean<TesteBean> csvToBean = new CsvToBean<>();
List<TesteBean> beanList = csvToBean.parse(strategy, csvReader);
for (TesteBean bean : beanList) {
System.out.println(bean.getNome());
System.out.println(bean.getValor());
}
List<String[]> rows = csvReader.readAll();
System.out.println("******* Below this, works fine and prints the values!!");
for (String[] line : rows) {
System.out.println("Linha:");
for (int i = 0; i < line.length; i++) {
System.out.println(line[i]);
}
}
csvReader.close();
此外,这是我使用的javabean:
public class TesteBean {
@CsvBind
private String valor;
@CsvBind
private String nome;
public String getValor() {
return valor;
}
public String getNome() {
return nome;
}
public void setValor(String valor) {
this.valor = valor;
}
public void setNome(String nome) {
this.nome = nome;
}
}
此外,这是我的CSV文件:
NOME; VALOR;
Cliente; Valor_Cliente;
Financeira; Valor_Financeira;
但是当我检查beanList变量中的值时,它们都是NULL。
有什么想法吗? 非常感谢