java 通过获取匹配的行并返回差异来比较CSV文件

bsxbgnwa  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(86)

我有三个CSV文件,我想用它来逐行比较。第一个文件(QR.csv)不是很有用。事实上,比较将在另外两个文件(QRSegmentQuestion + QRSegmentQuestion NCL)上完成。我的目标是在两个文件之间进行逐行比较。这就是为什么我设法读取文件并将它们集成到ArrayList中。
另一个问题是,这两个文件中的行顺序不一样,所以我必须用不同的方式选择它们。我在ArrayList中有四个元素,可以让我在两个文件中找到相同的行:“Question”,“Segment”,“NomChamp”,“TypeChamp”。
回到第一个CSV文件的目的,检查“Questions”元素是否存在(这部分对我来说是可选的)。
一旦我确定了两个文档中的文件行,我想与其他剩余的元素进行比较,并返回差异,如果相等则什么也不说。
比较类:

package ComparePivotSegment;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ComparePivotSegment {
    
    public static void main(String[]args) {
        //Declare all path file
        String sPathQr = "C:\\Users\\dervis.sahin\\Downloads\\QR.csv";
        String sPathPivotSegmentNcl = "C:\\Users\\dervis.sahin\\Downloads\\PivotSegmentQuestionNCL.csv";
        String sPathPivotSegment = "C:\\Users\\dervis.sahin\\Downloads\\PivotSegmentQuestion.csv";
        
        //Read and store the data from each file
        ArrayList <FileQr> qrList = readAndPrintFileQr(sPathQr);
        ArrayList <PivotSegmentNcl> PivotSegmentNclList = readAndPrintPivotSegmentNcl(sPathPivotSegmentNcl);
        ArrayList <PivotSegment> PivotSegmentList = readAndPrintPivotSegment (sPathPivotSegment);

        //compare elements
    }
    
    //Method to read and print file QR
    private static ArrayList<FileQr> readAndPrintFileQr(String filePath){
        ArrayList<FileQr> qrList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))){
            String line = "";
            line = reader.readLine();
            while ((line = reader.readLine())!= null) {
                String[] values = line.split(";");
                if(values.length!=15) continue;
                String Dossier = values [0];
                String Question = values[1];
                String NomQuestion = values[2];
                String IndicateurRESTIT = values[3];
                String NomPremierServiceREST = values[4];
                String TypeAcces = values[5];
                String Description = values[6];
                String Fichier = values[7];
                String ContratServiceQR = values[8];
                String Application = values[9];
                String CodeBlocApplicatif = values[10];
                String IndicateurDeloc = values[11];
                String Objet = values [12];
                String Ressources = values[13];
                String LigneCodeSource = values [14];
                
                FileQr qr = new FileQr(Dossier, Question, NomQuestion, IndicateurRESTIT, NomPremierServiceREST, TypeAcces, Description, Fichier, ContratServiceQR, Application, CodeBlocApplicatif, IndicateurDeloc, Objet, Ressources, LigneCodeSource);
                qrList.add(qr);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return qrList;
    }

    //Method to read and print PivotSegmentQuestionNCL
    private static ArrayList<PivotSegmentNcl> readAndPrintPivotSegmentNcl(String filePath){
        ArrayList<PivotSegmentNcl> PivotSegmentNclList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader (new FileReader(filePath))){
            String line = "";
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(";");
                if (!(values.length>=13))continue;
                String Dossier = values[0];
                String Question = values[1];
                String NomQuestion = values[2];
                String Segment = values[3];
                String Sens = values[4];
                String PositionChamp = values[5];
                String NomChamp = values[6];
                String TypeChamp = values[7];
                String LongueurChamp = values[8];
                String Description = values[9];
                String LibelleSegment = values[10];
                String NomChampJava = values[11];
                String NomSegmentJava = values[12];
                String ChampUtilise = "";
                String ChampBanalise = "";
            
                PivotSegmentNcl ncl = new PivotSegmentNcl(Dossier, Question, NomQuestion, Segment, Sens, PositionChamp, NomChamp, TypeChamp, LongueurChamp, Description, LibelleSegment, NomChampJava, NomSegmentJava, ChampUtilise, ChampBanalise );
                PivotSegmentNclList.add(ncl);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();        
        }
        return PivotSegmentNclList;
    }

    //Method for read and print PivotSegmentQuestion
    private static ArrayList<PivotSegment> readAndPrintPivotSegment(String filePath){
        ArrayList<PivotSegment> PivotSegmentList = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line = "";
            while((line = reader.readLine())!=null) {
                String [] values = line.split(";");
                if(!(values.length>=13)) continue;
                String Dossier = values [0];
                String Question = values [1];
                String NomQuestion = values[2];
                String Segment = values[3];
                String Sens = values[4];
                String PositionChamp = values[5];
                String NomChamp = values[6];
                String TypeChamp = values[7];
                String LongueurChamp = values[8];
                String Description = values[9];
                String LibelleSegment = values[10];
                String NomChampJava = values[11];
                String NomSegmentJava = values[12];
                String ChampUtilise = "";
                String ChampBanalise = "";
                
                PivotSegment pivotsegment = new PivotSegment(Dossier, Question, NomQuestion, Segment, Sens, PositionChamp, NomChamp, TypeChamp, LongueurChamp, Description, LibelleSegment, NomChampJava, NomSegmentJava, ChampUtilise, ChampBanalise);
                PivotSegmentList.add(pivotsegment);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return PivotSegmentList;
    }
    //Method to compare 
}

字符串
文件Qr类:

package ComparePivotSegment;

public class FileQr {

    public String Dossier;
    public String Question;

    public String getDossier() {
        return Dossier;
    }

    public void setDossier(String dossier) {
        Dossier = dossier;
    }

    public String getQuestion() {
        return Question;
    }

    public void setQuestion(String question) {
        Question = question;
    }

    public String getNomQuestion() {
        return NomQuestion;
    }

    public void setNomQuestion(String nomQuestion) {
        NomQuestion = nomQuestion;
    }

    public String getIndicateurRESTIT() {
        return IndicateurRESTIT;
    }

    public void setIndicateurRESTIT(String indicateurRESTIT) {
        IndicateurRESTIT = indicateurRESTIT;
    }

    public String getNomPremierServiceREST() {
        return NomPremierServiceREST;
    }

    public void setNomPremierServiceREST(String nomPremierServiceREST) {
        NomPremierServiceREST = nomPremierServiceREST;
    }

    public String getTypeAcces() {
        return TypeAcces;
    }

    public void setTypeAcces(String typeAcces) {
        TypeAcces = typeAcces;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getFichier() {
        return Fichier;
    }

    public void setFichier(String fichier) {
        Fichier = fichier;
    }

    public String getContratServiceQR() {
        return ContratServiceQR;
    }

    public void setContratServiceQR(String contratServiceQR) {
        ContratServiceQR = contratServiceQR;
    }

    public String getApplication() {
        return Application;
    }

    public void setApplication(String application) {
        Application = application;
    }

    public String getCodeBlocApplicatif() {
        return CodeBlocApplicatif;
    }

    public void setCodeBlocApplicatif(String codeBlocApplicatif) {
        CodeBlocApplicatif = codeBlocApplicatif;
    }

    public String getIndicateurDeloc() {
        return IndicateurDeloc;
    }

    public void setIndicateurDeloc(String indicateurDeloc) {
        IndicateurDeloc = indicateurDeloc;
    }

    public String getObjet() {
        return Objet;
    }

    public void setObjet(String objet) {
        Objet = objet;
    }

    public String getRessource() {
        return Ressource;
    }

    public void setRessource(String ressource) {
        Ressource = ressource;
    }

    public String getLigneCodeSource() {
        return LigneCodeSource;
    }

    public void setLigneCodeSource(String ligneCodeSource) {
        LigneCodeSource = ligneCodeSource;
    }

    public String NomQuestion;
    public String IndicateurRESTIT;
    public String NomPremierServiceREST;
    public String TypeAcces;
    public String Description;
    public String Fichier;
    public String ContratServiceQR;
    public String Application;
    public String CodeBlocApplicatif;
    public String IndicateurDeloc;
    public String Objet;
    public String Ressource;
    public String LigneCodeSource;

    public FileQr(String Dossier, String Question, String NomQuestion, String IndicateurRESTIT, String NomPremierServiceREST, String TypeAcces, String Description, String Fichier, String ContratServiceQR, String Application, String CodeBlocApplicatif, String IndicateurDeloc, String Objet, String Ressource, String LigneCodeSource) {
        this.Dossier = Dossier;
        this.Question = Question;
        this.NomQuestion = NomQuestion;
        this.IndicateurRESTIT = IndicateurRESTIT;
        this.NomPremierServiceREST = NomPremierServiceREST;
        this.TypeAcces = TypeAcces;
        this.Description = Description;
        this.Fichier = Fichier;
        this.ContratServiceQR = ContratServiceQR;
        this.Application = Application;
        this.CodeBlocApplicatif = CodeBlocApplicatif;
        this.IndicateurDeloc = IndicateurDeloc;
        this.Objet = Objet;
        this.Ressource = Ressource;
        this.LigneCodeSource = LigneCodeSource;
    }

}


SegmentQuestion类

package ComparePivotSegment;

public class PivotSegment {

    public String getDossier() {
        return Dossier;
    }

    public void setDossier(String dossier) {
        Dossier = dossier;
    }

    public String getQuestion() {
        return Question;
    }

    public void setQuestion(String question) {
        Question = question;
    }

    public String getNomQuestion() {
        return NomQuestion;
    }

    public void setNomQuestion(String nomQuestion) {
        NomQuestion = nomQuestion;
    }

    public String getSegment() {
        return Segment;
    }

    public void setSegment(String segment) {
        Segment = segment;
    }

    public String getSens() {
        return Sens;
    }

    public void setSens(String sens) {
        Sens = sens;
    }

    public String getPositionChamp() {
        return PositionChamp;
    }

    public void setPositionChamp(String positionChamp) {
        PositionChamp = positionChamp;
    }

    public String getNomChamp() {
        return NomChamp;
    }

    public void setNomChamp(String nomChamp) {
        NomChamp = nomChamp;
    }

    public String getTypeChamp() {
        return TypeChamp;
    }

    public void setTypeChamp(String typeChamp) {
        TypeChamp = typeChamp;
    }

    public String getLongueurChamp() {
        return LongueurChamp;
    }

    public void setLongueurChamp(String longueurChamp) {
        LongueurChamp = longueurChamp;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getLibelleSegment() {
        return LibelleSegment;
    }

    public void setLibelleSegment(String libelleSegment) {
        LibelleSegment = libelleSegment;
    }

    public String getNomChampJava() {
        return NomChampJava;
    }

    public void setNomChampJava(String nomChampJava) {
        NomChampJava = nomChampJava;
    }

    public String getNomSegmentJava() {
        return NomSegmentJava;
    }

    public void setNomSegmentJava(String nomSegmentJava) {
        NomSegmentJava = nomSegmentJava;
    }

    public String getChampUtilise() {
        return ChampUtilise;
    }

    public void setChampUtilise(String champUtilise) {
        ChampUtilise = champUtilise;
    }

    public String getChampBanalise() {
        return ChampBanalise;
    }

    public void setChampBanalise(String champBanalise) {
        ChampBanalise = champBanalise;
    }

    public String Dossier;
    public String Question;
    public String NomQuestion;
    public String Segment;
    public String Sens;
    public String PositionChamp;
    public String NomChamp;
    public String TypeChamp;
    public String LongueurChamp;
    public String Description;
    public String LibelleSegment;
    public String NomChampJava;
    public String NomSegmentJava;
    public String ChampUtilise;
    public String ChampBanalise;
        
    public PivotSegment (String Dossier, String Question, String NomQuestion, String Segment, String Sens, String PositionChamp, String NomChamp, String TypeChamp, String LongueurChamp, String Description, String LibelleSegment, String NomChampJava, String NomSegmentJava, String ChampUtilise, String ChampBanalise) {
        this.Dossier = Dossier;
        this.Question = Question;
        this.NomQuestion = NomQuestion;
        this.Segment = Segment;
        this.Sens = Sens;
        this.PositionChamp = PositionChamp;
        this.NomChamp = NomChamp;
        this.TypeChamp = TypeChamp;
        this.LongueurChamp = LongueurChamp;
        this.Description = Description;
        this.LibelleSegment = LibelleSegment;
        this.NomChampJava = NomChampJava;
        this.NomSegmentJava = NomSegmentJava;
        this.ChampUtilise = ChampUtilise;
        this.ChampBanalise = ChampBanalise;
    }

}


Segment
Qr.csv示例:档案;问题; NomQuestion;指示RESTIT; NomPremierServiceREST;类型访问;说明;菲谢尔;网站Map申请;代码块应用程序Deloc; Objet;雷索斯; LigneCodeSource dosssier1;问题1; nomquestion 1; indicateur1; nompremier1; typeacces1;预防1; fichier1; contratserviceqr1;应用程序1; codebloc1; IndicateurDeloc1;目标1;资源1; LigneCodeSource 1档案2;问题2; nomquestion 2; indicateur2; nompremier2; typeacces2;预防措施2; fichier2; contratserviceqr2;应用程序2; codebloc2; IndicateurDeloc2;目标2;资源2; LigneCodeSource 2档案3;问题3; nomquestion 3; indicateur3; nompremier3; typeacces3;预防措施3; fichier3; contratserviceqr3;应用程序3; codebloc3; IndicateurDeloc3;目标3;资源3; LigneCodeSource 3档案4;问题4; nomquestion 4; indicateur4; nompremier4; typeacces4;预防措施4; fichier4; contratserviceqr4;应用程序4; codebloc4; IndicateurDeloc4;目标4;资源4; LigneCodeSource 4档案5;问题5; nomquestion 5; indicateur5; nompremier5; typeacces5;预防5; fichier5; contratserviceqr5;应用程序5; codebloc5; IndicateurDeloc5;目标5;资源5; LigneCodeSource5
数据段Ncl:档案;问题; NomQuestion;分段; Sens;位置冠军; NomChamp;类型Champ; Longueur Champ;描述Champ; LibelleSegment; NomChampJava; NomSegmentJava; ChampUtilise; ChampBanalise档案1; 1;提名1;段1;感官1;位置Champ 1; NomChamp1; Type Champ1; LongueurChamp1;说明Champ 1; LibelleSegment1; NomChampJava1; NomSegmentJava1; ChampUtilise1; ChampBanalise 1档案2; 2;提名2;第2段; Sens2;位置Champ 2; NomChamp2; TypeChamp2; LongueurChamp2;说明Champ 2; LibelleSegment2; NomChampJava2; NomSegmentJava2; ChampUtilise2; ChampBanalise 2档案3; 3;提名3;第3段; Sens3;位置Champ 3; NomChamp3; Type Champ3; LongueurChamp3;说明Champ 3; LibelleSegment3; NomChampJava3; NomSegmentJava3; ChampUtilise3; ChampBanalise档案4; 104;提名4;第4段; Sens4;位置Champ 4; NomChamp4; TypeChamp4; LongueurChamp4;说明Champ 4; LibelleSegment4; NomChampJava4; NomSegmentJava4; ChampUtilise4; ChampBanalise 4档案5; 105; 5名;第5段; Sens5;位置Champ 5; NomChamp5; Type Champ5; LongueurChamp5;说明Champ 5; LibelleSegment5; NomChampJava5; NomSegmentJava5; ChampUtilise5; ChampBanalise5
业务细分问题:档案;问题; NomQuestion;分段; Sens;位置冠军; NomChamp;类型Champ; Longueur Champ;描述Champ; LibelleSegment; NomChampJava; NomSegmentJava; ChampUtilise; ChampBanalise档案1; 1;提名1;段1;感官1;位置Champ 1; NomChamp1; Type Champ1; LongueurChamp1;说明nChamp 1; LibelleSegment1; NomChampJava1; NomSegmentJava1; ChampUtilise1; ChampBanalise 1档案2; 2;提名2;第2段; Sens2;位置Champ 2; NomChamp2; TypeChamp2; LongueurChamp2;说明Champ 2; LibelleSegment2; NomChampJava2; NomSegmentJava2; ChampUtilise2; ChampBanalise 2档案3; 3;提名3;第3段; Sens3;位置Champ 3; NomChamp3; Type Champ3; LongueurChamp3;描述Champ 3; LibelleSegment3; NomChampJava3; NomSegmentJava3; ChampUtilise3; ChampBanalise档案4; 104;提名4;第4段; Sens4;位置Champ 4; NomChamp4; TypeChamp4; LongueurChamp4;说明Champ 4; LibellleSegment4; NomChampJava4; NomSegmentJava4; ChampUtilise4; ChampBannalise 4档案5; 105; 5名;第5段; Sens5;位置Champ 5; NomChamp5; Type Champ5; LongueurChamp5;说明Champ 5; LibelleSegment5; NomChampJava5; NomSeggmentJava5; ChampUtilise5; ChampBanalise5

fdbelqdn

fdbelqdn1#

我无法在我的机器上测试它,所以可能需要一些小的修复:

//compare elements
    ArrayList <PivotSegment> eqElements = new ArrayList<PivotSegment>();
    PivotSegmentList.stream().forEach(line -> arePivotSegmentsEqual(line,PivotSegmentNclList,qrList) ? eqElements.add(line))
}

private static boolean arePivotSegmentsEqual(PivotSegmentQuestion segementQuestion, ArrayList<PivotSegmentQuestionNCL> segmentQuestionNclList; ArrayList <FileQr> qrList){
    for (PivotSegmentQuestionNCL ncl : segmentQuestionNclList) {
        if(isNCLEqualSegementQuestion(ncl, segementQuestion)){
            for (FileQr qr : segmentQuestionNclList) {
                if(isSegementQuestionEqualQr(segementQuestion, qr)){
                    return true;
                }
        }
    }
    }
    return null
}

private static PivotSegmentQuestion isNCLEqualSegementQuestion(PivotSegmentQuestionNCL nc, PivotSegmentQuestion segementQuestion){
    return segementQuestion.getQuestion() == nc.getQuestion && 
    segementQuestion.getSegment() == nc.getSegment() && 
    segementQuestion.getTypeChamp() == nc.getTypeChamp() && 
    segementQuestion.getNomChamp() == nc.getNomChamp() 
    ? true : false;
}

private static PivotSegmentQuestion isSegementQuestionEqualQr(PivotSegmentQuestion segementQuestion, FileQr qr){
    return segementQuestion.getQuestion() == qr.getQuestion() && 
    segementQuestion.getSegment() == qr.getSegment() && 
    segementQuestion.getTypeChamp() == qr.getTypeChamp() && 
    segementQuestion.getNomChamp() == qr.getNomChamp() 
    ? true : false;
}

字符串
这应该可以帮助你比较这三个列表。我不知道你说的FieldName和FieldType是什么意思,所以我用了getNomChamp和getTypeChamp。我希望这个解决方案可以解决你的问题。
对于未来,有两件事可能会对你有所帮助。首先是Lombok项目(没有更多的getter和setter,只需将Lombok添加到项目中,并且不需要更多的getter和setter注解和序列化,这使得阅读CSV文件更容易)。

相关问题