【问题标题】:Simple CSV File comparison in JAVAJAVA中的简单CSV文件比较
【发布时间】:2019-03-18 14:03:11
【问题描述】:

我正在使用下面的代码来比较两个 CSV 的 file1 和 file2。其中 file1 是需要始终与 file2 进行比较的文件,它完成了我需要的部分工作,但并没有完成所有工作。我这里主要需要3个对比。

1> 与 file2 相比,file1 中缺少行 --> 不工作 {还显示 file2 的记录,这不工作}

2> file2 中不存在的 file1 中的附加行 --> 工作

3> 与 file2 相比,file1 中的数据不匹配 --> 工作{同时显示 file1 和 file2 行,这不起作用}

此外,我需要在 {} 中编写的 cmets 才能正常工作。

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();
            }
    }
}

【问题讨论】:

  • 遵循 Java 命名约定,亲爱的上帝,你想在这里做什么?
  • @MS90:基本上比较两个csv文件,尽量达到点1,2,3

标签: java arraylist bufferedreader


【解决方案1】:

像这样简化你的代码(对 al2 做同样的事情):

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

然后从文件中查找不在另一个文件中的行(对 al1 和 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,但你可以轻松地计算它们,将它们保存到文件等。

【讨论】:

    【解决方案2】:

    您可以尝试使用此工具来比较 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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多