我使用以下代码来比较两个CSV的file1和file2。其中 file1 是需要始终与 file2 进行比较的文件,它执行我需要的部分工作,但不能完成所有工作。我在这里主要需要3个比较。
1> 与 file2 相比,file1 中缺少行 -->不起作用 {还显示文件 2 的记录,该文件不起作用}
2> 文件1中不存在的附加行 -->正在工作
3> 与 file2 相比,file1 中的数据不匹配 --> 正在工作 {同时显示 file1 和 file2 行,这不起作用}
此外,我需要在{}中编写的注释才能正常工作。
package com.mkyong;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class CSVComparison {
public static void main(String args[]) throws FileNotFoundException, IOException
{
String file1="qa_table_stats.csv";
String file2="prod_table_stats.csv";
String file3="TabStats_qa_prod.csv";
ArrayList al1=new ArrayList();
ArrayList al2=new ArrayList();
BufferedReader CSVFile1 = new BufferedReader(new FileReader(file1));
String dataRow1 = CSVFile1.readLine();
while (dataRow1 != null)
{
String[] dataArray1 = dataRow1.split("/n");
for (String item1:dataArray1)
{
al1.add(item1);
}
dataRow1 = CSVFile1.readLine(); // Read next line of data.
}
CSVFile1.close();
BufferedReader CSVFile2 = new BufferedReader(new FileReader(file2));
String dataRow2 = CSVFile2.readLine();
while (dataRow2 != null)
{
String[] dataArray2 = dataRow2.split("/n");
for (String item2:dataArray2)
{
al2.add(item2);
}
dataRow2 = CSVFile2.readLine(); // Read next line of data.
}
CSVFile2.close();
String bs = null;
for(Object o: al2)
{
bs = o.toString();
al1.remove(bs); // Checks for Additional Row in al1 and difference in rows in al1,
// but does not check for missing rows which are in bs but not in al1
}
白板的微信
慕虎7371278
POPMUISE
随时随地看视频慕课网APP
相关分类