通过获取匹配行并返回 JAVA 中的差异来比较 csv 文件

问题描述 投票:0回答:1

我有 3 个 csv 文件,我想用它们来逐行比较。第一个文件 (QR.csv) 不是很有用。事实上,比较将在其他 2 个文件(PivotSegmentQuestion + PivotSegmentQuestionNCL)上进行。我的目标是对两个文件进行逐行比较。这就是为什么我设法读取这些文件并将它们集成到 ArrayList 中。

另一个问题是两个文件中的行顺序不同,因此我必须以不同的方式选择它们。我的 ArrayList 中有 4 个元素,它们允许我在两个文件之间找到相同的行:“Question”、“Segment”、“FieldName”、“FieldType”。

回到第一个csv文件的目的,就是检查“Questions”元素是否存在(这部分对我来说是可选的)。

一旦识别了两个文档中的文件行,我想与其他剩余元素进行比较并返回差异,如果相等则什么也不说。

预先感谢您的帮助,如果您有任何不清楚的地方,请告诉我:)

比较班级:

package ComparePivotSegment;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
//import java.io.PrintWriter;
//import java.io.UnsupportedEncodingException;


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


PivotSegment问题类

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

PivotSegmentQuestionNCL 类与上面的相同

预先感谢您的帮助,如果您有任何不清楚的地方,请告诉我:)

java csv comparison row
1个回答
0
投票

您好,M0rph5us,您可以提供 csv 以便我可以先在我的机器上测试它吗?

© www.soinside.com 2019 - 2024. All rights reserved.