我正在一个项目中,我需要比较2个CSV的相等性。它在每个CSV中只有一行,具有多列,并以不同的形式报告列名称和值的差异。例如:
CSV 1 :
Name Roll No Dept
Brij 1 IT
CSV 2 :
Name Roll No Dept
Brij 2 IT
I need to report the diff as
Roll No 1 does not match Roll No 2 i.e. Col name and value in both csvs
My work till now :
@Test
public void f() {
try {
FileReader reader1 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader.csv"));
FileReader reader2 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader - Copy.csv"));
CSVReader csvReader1 = new CSVReader(reader1);
CSVReader csvReader2 = new CSVReader(reader2);
String[] nextRecordReader1;
String[] nextRecordReader2;
// we are going to read data line by line
while ((nextRecordReader1 = csvReader1.readNext()) != null
&& (nextRecordReader2 = csvReader2.readNext()) != null) {
checkEquality(nextRecordReader1, nextRecordReader2);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
int n = s1.length;
if (n != s2.length) {}
for (int i = 0; i < n; i++) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println(s1[i] + " is not equal to " + s2[i]);
}
}
}
这给了我以下结果: 1不等于2
我无法提及列名。我该怎么办?
答案 0 :(得分:0)
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
String[] headers= new String[]{"Name","Roll No","Dept"};
int n = s1.length;
if (n != s2.length) {}
for (int i = 0; i < n; i++) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println(headers[i]+" "+s1[i] + " is not equal to " headers[i]+" "+ s2[i]);
}
}
}
您需要一个用于保存列标题的集合,并使用该集合基于索引获取标题。
答案 1 :(得分:0)
enter code here
public class SampleTest {
int rowCountCounter;
String[] headers1;
String[] headers2;
@Test
public void f() {
try {
FileReader reader1 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader.csv"));
FileReader reader2 = new FileReader(
new File(System.getProperty("user.dir") + "\\src\\test\\resources\\csvFile\\Reader - Copy.csv"));
CSVReader csvReader1 = new CSVReader(reader1);
CSVReader csvReader2 = new CSVReader(reader2);
String[] nextRecordReader1;
String[] nextRecordReader2;
rowCountCounter=0;
// we are going to read data line by line
while ((nextRecordReader1 = csvReader1.readNext()) != null
&& (nextRecordReader2 = csvReader2.readNext()) != null) {
if(rowCountCounter==0) {
headers1=nextRecordReader1;
headers2=nextRecordReader2;
}
checkEquality(nextRecordReader1, nextRecordReader2);
rowCountCounter++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void checkEquality(String[] s1, String[] s2) {
if (s1 == s2) {}
if (s1 == null || s2 == null) {}
int n = s1.length;
if (n != s2.length) {
System.out.println("Header in CSV1 " + n + " is not equal to header in CSV2 " + s2.length);
}
boolean headerFlag=true;
for (int i = 0; i < n; i++) {
if(rowCountCounter==0) {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println("CSV1 header "+s1[i] + " is not equal to CSV2 header " + s2[i]);
headerFlag=false;
}
}else {
if (!s1[i].trim().equals(s2[i].trim())) {
System.out.println("CSV1 "+headers1[i] +" cellvalue "+" \"" +s1[i] +"\"" +" is not equal to CSV2 " + headers2[i]+" value \""+s2[i]+"\"");
headerFlag=false;
}
// else {
// System.out.println("CSV data is equal");
// }
}
}
if(headerFlag==true && rowCountCounter==0) {
System.out.println("CSV headers are equal.Below are the headers");
for(int k=0;k<s1.length;k++) {
System.out.print(s1[k]+" ");
}
}
if(headerFlag==true && rowCountCounter!=0) {
System.out.println("CSV data is equal.");
}
}
}