Comparaison de 2 fichiers texte (pb boucle while je pense)

tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009 - 7 mai 2009 à 17:15
tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009 - 11 mai 2009 à 12:20
Bonjour,

Jessaye de comparer 2 fichiers texte ligne par ligne.Chaque ligne est decoupee (split(";")) et chaque mot est stocké dans un array.
Ensuite je compare le contenu des array, en fonction des "colonnes" ou se trouvent les elements a comparer (au cas ou les champs a comparer ne soient pas dans les memes colonnes dans les 2 fichiers...)

Ca split, ca stocke. Mais le hic c'est que a la comparaison, il reste bloque sur la premiere ligne (voir contenu du fichier de LOG): il affiche 4 fois la meme ligne pour la ligne 1, alors que la ligne 2 varie bien au fur et a mesure de la boucle.
Ca doit venir de ma boucle

voici le code

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;
import javax.imageio.*;
import javax.imageio.stream.FileImageOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import java.security.*;
import java.security.spec.*;
import java.math.*;
import javax.imageio.*;
import javax.imageio.stream.FileImageOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.*;
import java.util.regex.Pattern;


/*Comparer 2 fichiers Excel*/


 


public class CINTEL extends JFrame{


        Dialog d_choosefile;
        JDialog d_choosecolumns;
        JButton nextBTN;
        JButton choice1BTN;
        JButton choice2BTN;
        JButton choiceLogBTN;
        JTextField txt_choice1;
        JTextField txt_choice2;
        JTextField txt_choiceLog;
        static String[] arrayString1;
        static String[] arrayString2;
        String[] col1Strings = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        String st_col1;//ND fichier 1
        String st_col2;
        String st_col3;//ND fichier 2
        String st_col4;//date fichier 1
        String st_col5;//date fichier 2
        String REGEX = ";";


       
  
        public CINTEL(){
          
            //getContentPane();
            System.out.println("in CINTEL()");
            //addMouseListener(this);
            d_choosefile = new JDialog(this, "Choix des fichiers...", true);
            nextBTN = new JButton(">>");
            nextBTN.addActionListener( new ActionListener() {
                     public void actionPerformed(ActionEvent e) {
                          System.out.println("in >>");
                          // typedText = txtTitel.getText();
                     }
            });
            txt_choice1= new JTextField(30);
            txt_choice2= new JTextField(30);
            txt_choiceLog= new JTextField(30);       
            choice1BTN = new JButton("...");
            choice2BTN = new JButton("...");
            choiceLogBTN = new JButton("...");
            choice1BTN.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                          System.out.println("in Choice1");
                          JFileChooser chooser = new JFileChooser("./");
                          chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                          chooser.setFileFilter(new Image_Filter());
                          //int returnVal = chooser.showOpenDialog(this);
                          int returnVal = chooser.showOpenDialog(getParent());
                          if (returnVal == JFileChooser.APPROVE_OPTION){
                                 File directory = chooser.getSelectedFile();
                                 try{
                                     String ext  = Image_Filter.getExtension(directory);
                                     String name = directory.getName();
                                     String path = directory.getPath();
                                     path = path.substring(0,path.length()-name.length()-1);     
                                     name = name.substring(0, name.length()-4); 
                                     String pathname = directory.getPath();
                                     txt_choice1.setText(pathname);                           
                                }catch(Exception except) {
                                     System.out.println("Problem HERE!");
                                }//end catch
                         }else{                               
                                System.out.println("Problem in IF!in CHOICE1");
                         }//end if
                      }});
           choice2BTN.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             System.out.println("in Choice2");
                             JFileChooser chooser = new JFileChooser("./");
                             chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                             chooser.setFileFilter(new Image_Filter());
                             //int returnVal = chooser.showOpenDialog(this);
                             int returnVal = chooser.showOpenDialog(getParent());
                             if (returnVal == JFileChooser.APPROVE_OPTION){
                                 File directory = chooser.getSelectedFile();
                                 try{
                                     String ext  = Image_Filter.getExtension(directory);
                                     String name = directory.getName();
                                     String path = directory.getPath();
                                     path = path.substring(0,path.length()-name.length()-1);     
                                     name = name.substring(0, name.length()-4);
                                     String pathname = directory.getPath();
                                     txt_choice2.setText(pathname);                               
                                }catch(Exception except) {
                                     System.out.println("Problem HERE!");
                                }//end catch
                         }else{                               
                                System.out.println("Problem in IF! in CHOICE2");
                         }//end if
                    }     
           });
          choiceLogBTN.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             System.out.println("in Choice2");
                             JFileChooser chooser = new JFileChooser("./");
                             chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                             chooser.setFileFilter(new Image_Filter());
                             //int returnVal = chooser.showOpenDialog(this);
                             int returnVal = chooser.showOpenDialog(getParent());
                             if (returnVal == JFileChooser.APPROVE_OPTION){
                                 File directory = chooser.getSelectedFile();
                                 try{
                                     String ext  = Image_Filter.getExtension(directory);
                                     String name = directory.getName();
                                     String path = directory.getPath();
                                     path = path.substring(0,path.length()-name.length()-1);     
                                     name = name.substring(0, name.length()-4);
                                     //String pathname = directory.getPath();
                                     txt_choiceLog.setText(path);                               
                                }catch(Exception except) {
                                     System.out.println("Problem HERE!");
                                }//end catch
                             }else{                               
                                System.out.println("Problem in IF!in CHOICE LOG");
                             }//end if
                    }
          });
          nextBTN.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             System.out.println("in NEXT >>");
                             //d_choosecolumns.setVisible(true);
                             showDialog2();
                    }
          });
          JPanel choix1Panel = new JPanel();
          JPanel choix2Panel = new JPanel();
          JPanel choixLogPanel = new JPanel();
          //JPanel labelsPanel = new JPanel();
          JPanel lstPanel = new JPanel();
          JLabel ls1 = new JLabel("Fichier Export Hebdo CINTEL");
          JLabel ls2 = new JLabel("Fichier a Renseigner(TREIN)");
          JLabel ls3 = new JLabel("Fichier LOG");


          choix1Panel.add("West", ls1);
          choix1Panel.add("Center", txt_choice1);
          choix1Panel.add("East", choice1BTN);
          choix2Panel.add("West", ls2);
          choix2Panel.add("Center", txt_choice2);
          choix2Panel.add("East", choice2BTN);
          choixLogPanel.add("West", ls3);
          choixLogPanel.add("Center", txt_choiceLog);
          choixLogPanel.add("East", choiceLogBTN);
          lstPanel.add(choix1Panel, BorderLayout.CENTER);      
          lstPanel.add(choix2Panel,  BorderLayout.CENTER);       
          lstPanel.add(choixLogPanel,  BorderLayout.CENTER);      
          //lstPanel.add(nextBTN, BorderLayout.PAGE_END);
          lstPanel.add(nextBTN, new GridBagConstraints(1, 4, 1, 2, 0, .25,GridBagConstraints.PAGE_END, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 0, 0));
    
          d_choosefile.add(lstPanel);
          d_choosefile.setSize(600,200);
          d_choosefile.setVisible(true);


 




      }//end CINTEL()




      public static void main(String[] args) {
           System.out.println("in MAIN");
           CINTEL cintel = new CINTEL();
           cintel.setSize(600,600);
           cintel.setTitle("ROBOT Remplissage fichier Excel");
           ImageIcon img= new ImageIcon("smiley.gif");
           cintel.setIconImage(img.getImage());
           //ajout du panel daffichage graphique
           cintel.getContentPane().add(new MyComponent());
           cintel.setDefaultCloseOperation(EXIT_ON_CLOSE);
           cintel.setVisible(true);


     }//end main()




public void showDialog2(){
         System.out.println("in SHOWDIALOG 2");
         d_choosefile.setVisible(false);
         d_choosecolumns = new JDialog(this, "Choix des colonnes...", true);
         JPanel p_col = new JPanel();
         JLabel lbcol1 = new JLabel("Champs fichier 1 (n° de ND)");
          JLabel lbcol2 = new JLabel("Colonnes à remplir fichier 2");
          JLabel lbcol3 = new JLabel("Champs fichier 2 (n° de ND)");
          JLabel lbcol4 = new JLabel("Champs fichier 1 (Date)");
          JLabel lbcol5 = new JLabel("Champs fichier 2 (Date)");
          //JLabel ls3 = new JLabel("Fichier LOG");
          //String[] col1Strings = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
          //Create the combo box, select item at index 4.
          JComboBox col1List = new JComboBox(col1Strings);
          col1List.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             //System.out.println("in NEXT >>");
                            JComboBox cb = (JComboBox)e.getSource();
                            st_col1 = (String)cb.getSelectedItem();
                            System.out.println("st_col1=  "+ st_col1);


                    }
          });
          JComboBox col2List = new JComboBox(col1Strings);
          col2List.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             //System.out.println("in NEXT >>");
                            JComboBox cb = (JComboBox)e.getSource();
                            st_col2 = (String)cb.getSelectedItem();
                            System.out.println("st_col2=  "+ st_col2);


                    }
          });
          JComboBox col3List = new JComboBox(col1Strings);
          col3List.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             //System.out.println("in NEXT >>");
                            JComboBox cb = (JComboBox)e.getSource();
                            st_col3 = (String)cb.getSelectedItem();
                            System.out.println("st_col3=  "+ st_col3);


                    }
          });
          JComboBox col4List = new JComboBox(col1Strings);
          col4List.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             //System.out.println("in NEXT >>");
                            JComboBox cb = (JComboBox)e.getSource();
                            st_col4 = (String)cb.getSelectedItem();
                            System.out.println("st_col4=  "+ st_col4);


                    }
          });
          JComboBox col5List = new JComboBox(col1Strings);
          col5List.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             //System.out.println("in NEXT >>");
                            JComboBox cb = (JComboBox)e.getSource();
                            st_col5 = (String)cb.getSelectedItem();
                            System.out.println("st_col5=  "+ st_col5);


                    }
          });
   JButton btn_ok = new JButton("OK");
   btn_ok.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                             System.out.println("in OK >>");
                             LineComparator();
                            
                    }
   });
            
   p_col.add("North", lbcol1);
   p_col.add("North", col1List);
   p_col.add("North", lbcol3);
   p_col.add("North", col3List);
   p_col.add("North", lbcol4);
   p_col.add("North", col4List);
   p_col.add("North", lbcol5);
   p_col.add("North", col5List);
   p_col.add("North", lbcol2);
   p_col.add("North", col2List);


   d_choosecolumns.setLayout(new BorderLayout(10, 10));
   d_choosecolumns.add(p_col, BorderLayout.CENTER);
   d_choosecolumns.add(btn_ok, BorderLayout.PAGE_END);
   d_choosecolumns.setSize(450,200);
   d_choosecolumns.setVisible(true);


}//end showDialog2()


     static class MyComponent extends JComponent {
            MyComponent() {
                 System.out.println("in MYCOMPONENT()");
            }//end MyComponent
    }//end class MyComponent




public void LineComparator(){
       System.out.println("in LineComparator");
       //check ke les ND soient identiques (055232689  et 55232698 pas identiques!)
       //check les dates 15-avr-09 et 15/04/2009 pas dientiques


      // try {
    // Open log file
     //      PrintWriter plog = new PrintWriter( new FileWriter( txtchoiceLog+ "Change.log"));
           try{
                        System.out.println("dans le 1er TRY");
                        BufferedReader br1=new BufferedReader(new FileReader(txt_choice1.getText()));
                        BufferedReader br2=new BufferedReader(new FileReader(txt_choice2.getText()));
        FileWriter fstream = new FileWriter("LOG.txt");
        BufferedWriter out = new BufferedWriter(fstream);


                        try{
                              System.out.println("dans le 2eme TRY, les BUFFER sont lances");
                              String ligne1;
                              String ligne2;
                              int indexLigne=0;
                              arrayString1 = new String[10]; // a redimensionner avec le nombre de ligne exact !
                              arrayString2 = new String[10];
 
                              //while ((ligne1=br1.readLine())!=null){
                              while(true){
                              ligne1=br1.readLine();
                                   //System.out.println("dans le 1er WHILE");
                                   while  ((ligne2=br2.readLine())!=null){
                                            System.out.println("dans le 2eme WHILE");
                                            String[] elements1 = ligne1.split(";");
                                            //System.out.println("elements1" + elements1);
                                            String[] elements2 = ligne2.split(";");
                                            //System.out.println("elements2" + elements2);  




                                            for (int i = 0; i<elements1.length ; i++){
                                                    System.out.println("dans le FOR");
                                                        arrayString1[i] = elements1[i];
                                                        arrayString2[i] = elements2[i];
                                                        System.out.println("arrayString1[i]= " + arrayString1[i] + " arrayString2[i] = "+ arrayString2[i]);


                                            }//end for


                                            //for (int i = 0; i<arrayString1.length ; i++){
                                            if(arrayString1[Integer.parseInt(st_col1)].compareTo(arrayString2[Integer.parseInt(st_col3)]) <  0){
                                                  //System.out.println("ON COMPARE LES DONNEES: arrayString1[Integer.parseInt(st_col1)] = " + arrayString1[Integer.parseInt(st_col1)] + "  arrayString2[Integer.parseInt(st_col3)] = " + arrayString2[Integer.parseInt(st_col3)]);
                                                  //chaines identiques donc on renseigne la colonne
                                                 
                                                  out.write("OK:  " + indexLigne + ":  "+ ligne1 + " =  "+ ligne2 + System.getProperty("line.separator" ));
                                            }else{
                                                //System.out.println("Chaines Differentes !!!" + arrayString1[Integer.parseInt(st_col1)]+ " "+arrayString2[Integer.parseInt(st_col3)] + arrayString1[Integer.parseInt(st_col4)]+ " " +arrayString2[Integer.parseInt(st_col5)] );
                                                out.write("ERROR ! at " + indexLigne + " = " +ligne1 + " !=  "+ ligne2 + System.getProperty("line.separator" ));
                                            }//end if
                                            //}//end for
                                             indexLigne++;


                                    }//end while2
                                       br2.close();
                                       System.out.println("BR2 CLOSED");
                             }//end while1


//                                 br1.close();
//                                 System.out.println("BR1 CLOSED");


                        }catch(Exception e ){
                                 System.out.println(e.toString());
                                 System.out.println("PROBLEM HERE 1" + e.getMessage());
   
                        }//end try
                                 out.close();
                                 br1.close();
                                 System.out.println("BR1 CLOSED");
           }catch(Exception e ){
                  System.out.println(e.toString());
System.out.println("PROBLEM HERE 2");
           }//end try
        //}


 
}//end LineComparator








voila le contenu des 2 fichiers textes
file1
0158963256;FRANCK;15-avr-2009;PARIS
0146727711;ABDEL;20-avr-2009;DRANCY
0145217180;RICARDO;21-avr-2008;BOBIGNY
0206817132;MARCO;17-mai-2007;IVRY

file2
0130386482;FRANCIS;15-avr-2009;PARIS
0146727712;KADER;20-avr-2009;TOULOUSE
0145217180;RICARDO;22-avr-2008;BORDEAUX
0206817132;MARCO;17-mai-2007;IVRY

Merci de votre aide!

Tomski

(_.:~:._)

1 réponse

tomski Messages postés 48 Date d'inscription mercredi 24 mars 2004 Statut Membre Dernière intervention 24 décembre 2009
11 mai 2009 à 12:20
Personne qui pourrait m'aider?
Please !

Tomski

(_.:~:._)
0
Rejoignez-nous