第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JAVA中簡單的CSV文件比較

JAVA中簡單的CSV文件比較

白板的微信 2022-08-17 15:59:24
我使用以下代碼來比較兩個(gè)CSV的file1和file2。其中 file1 是需要始終與 file2 進(jìn)行比較的文件,它執(zhí)行我需要的部分工作,但不能完成所有工作。我在這里主要需要3個(gè)比較。1> 與 file2 相比,file1 中缺少行 -->不起作用 {還顯示文件 2 的記錄,該文件不起作用}2> 文件1中不存在的附加行 -->正在工作3> 與 file2 相比,file1 中的數(shù)據(jù)不匹配 --> 正在工作 {同時(shí)顯示 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         }
查看完整描述

2 回答

?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以嘗試使用此實(shí)用程序來比較 2 個(gè) 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());


查看完整回答
反對(duì) 回復(fù) 2022-08-17
?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

像這樣簡化你的代碼(對(duì)al2做同樣的事情):


ArrayList<String> al1=new ArrayList();

BufferedReader CSVFile1 = new BufferedReader(new FileReader(file1));

String dataRow1;

while ((dataRow1 = CSVFile1.readLine()) != null) {

    al1.add(dataRow1);

}

然后從文件中查找不在另一個(gè)文件中的行(對(duì) al1 vs al2 執(zhí)行相同的操作):


for (String s : al2) {

    if (!al1.contains(s)) {

        System.out.println("In file 2, but not in file 1: " + s);

    }

}

for 循環(huán)為您提供不同的行。我剛剛做了System.out.println,但你可以很容易地計(jì)算它們,將它們保存到文件等。


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 177 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)