Java中的简单CSV文件比较



我正在使用以下代码比较两个CSV的File1和file2。如果File1是需要将File 2与File2进行比较的文件,则它可以完成我需要但没有完成所有工作的部分工作。我主要需要在这里进行3个比较。

1>与file2->不起作用时丢失file1中的行{也显示不起作用的文件记录}

2> file1中不存在的file2中的其他行 ->工作

3>与file2->工作2-> Works {还显示File1和File2行相比,file1中的数据不匹配,这是不起作用的}

此外,我需要在{}内部写的评论才能正常工作。

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
         }
         int size=al1.size();
         System.out.println(size);
         System.out.println(bs);
         try
            {
                FileWriter writer=new FileWriter(file3);
                while(size!=0)
                {
                    size--;
                    writer.append(""+al1.get(size));
                    writer.append('n');
                }
                writer.flush();
                writer.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
    }
}

simplfy您的代码这样(对AL2进行相同的代码):

ArrayList<String> al1=new ArrayList();
BufferedReader CSVFile1 = new BufferedReader(new FileReader(file1));
String dataRow1;
while ((dataRow1 = CSVFile1.readLine()) != null) {
    al1.add(dataRow1);
}

然后从另一个文件中找到不在另一个文件的行(对于Al1 vs al2执行相同的操作):

for (String s : al2) {
    if (!al1.contains(s)) {
        System.out.println("In file 2, but not in file 1: " + s);
    }
}

for循环为您提供不同的行。我刚刚做了system.out.println,但是您可以轻松地计数它们,将它们保存为归档,等等。

您可以尝试使用此util进行比较2 CSV文件。

csv-comparator

CsvComparator comparator = CsvComparator.builder()
            .onColumns("email", "firstname", "lastname")
            .onCsvFiles(
                    "path/to/actual.csv",
                    "path/to/expected.csv")
            .byIdentityColumn("email")
            .build();
CsvComparisonResult result = comparator
            .saveDiffAt("build/csv-results")
            .perform();
// Check diff:
Assertions.assertTrue(result.hasDiff());
// Check addition, deletion, modification are also:
Assertions.assertTrue(result.hasRowAdded());
Assertions.assertTrue(result.hasRowDeleted());
Assertions.assertTrue(result.hasRowModified());

最新更新