【发布时间】:2021-02-01 13:54:20
【问题描述】:
我有一项工作来比较两个 CSV 的字段是:字符串 json 对象 json 对象日期字符串 字符串 如何打开文件并比较它们?到目前为止,我首先尝试了所有这些方法中的一个 CSV,那么接下来呢?:
public class MainApp {
public static void main(String[] args) throws IOException{
Collection<Object> lines = new ArrayList<>();
String filePath = ".\\sheet.csv";
Reader reader = Files.newBufferedReader(Paths.get(filePath));
CSVParser csvParser = new CSVParser(reader,CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
{
for (CSVRecord csvRecord : csvParser) {
String name = csvRecord.get("name");
String email = csvRecord.get("jobSpecification");
String phone = csvRecord.get("assignedProject");
String country = csvRecord.get("lastUpdated");
String id = csvRecord.get("id");
String comments = csvRecord.get("comments");
Data row = new Data(name, email, phone, country, id, comments);
lines.add(row);
}
}
for (Object d : lines) {
System.out.println(d.toString());
}
}
和对象:
public class Data {
String name;
String email ;
String phone ;
String country;
String id;
String comments;
public Data(String name, String email, String phone, String country, String id, String comments) {
this.name = name;
this.email = email;
this.phone = phone;
this.country = country;
this.id = id;
this.comments = comments;
}
@Override
public String toString() {
return "name: "+name+" "+" email: "+email+" phone: "+phone+" country: "+country+" id: "+id+" comments: "+comments;
}
}
所以现在我认为只剩下比较 CSV(我制作的)看起来像these
【问题讨论】:
-
不太清楚您要在这里完成什么。但是,最好有:
Collection<Data> lines = new ArrayList<>();和lines.add(new Data(name, email, phone, country, id, comments));。 -
@DevilsHnd 我正在尝试比较 cetween 2 csv,每个 csv 有两个实际上是 json 的字段,其余字段是数据、字符串、整数,所以我从每一行创建了一个对象..但接下来呢?