EDIFACT files comparison using Java

I want to compare two edifact files and find the differences between them as well using java. Please suggest the code. Sample file of Edifact is attached..

UNB+UNOA:1+005435656:1+006415160:1+060515:1434+00000000000778'
UNH+00000000000117+INVOIC:D:97B:UN'
BGM+380+342459+9'
DTM+3:20060515:102'
RFF+ON:521052'
NAD+BY+792820524::16++CUMMINS MID-RANGE ENGINE PLANT'
NAD+SE+005435656::16++GENERAL WIDGET COMPANY'
CUX+1:USD'
LIN+1++157870:IN'
IMD+F++:::WIDGET'
QTY+47:1020:EA'
ALI+US'
MOA+203:1202.58'
PRI+INV:1.179'
LIN+2++157871:IN'
IMD+F++:::DIFFERENT WIDGET'
QTY+47:20:EA'
ALI+JP'
MOA+203:410'
PRI+INV:20.5'
UNS+S'
MOA+39:2137.58'
ALC+C+ABG'
MOA+8:525'
UNT+23+00000000000117'
UNZ+1+00000000000778'

1 answer

  • answered 2018-02-21 05:49 K.D

    Here is what I am working on but it is not actually giving me result in a proper pattern.
    
    
    
    import java.io.*;
    import java.util.*;
    
    
    public class Compare_EDIFiles
    {
    
     public static void main (String[] args) throws java.io.IOException
        {
    
         //Getting the name of the files to be compared.
    
         FileReader fis1 = new FileReader ("filename1");
         FileReader fis2 = new FileReader ("filename2);
    
        String s1="";
        String s2="",s3="",s4="";
        String y="",z="";
    
        //Reading the contents of the files
    
        BufferedReader br = new BufferedReader (fis1);
        BufferedReader br1 = new BufferedReader (fis2);
    
       while((z=br1.readLine())!=null)
        s3+=z;
    
      while((y=br.readLine())!=null)
         s1+=y;
    
      System.out.println ();
    
         //String tokenizing
    
        int numTokens = 0;
        //String delim = ":";
        //String delim1 = ":";
        StringTokenizer st = new StringTokenizer (s1);
        String[] a = new String[10000];
        for(int l=0;l<10000;l++)
    
          {
            a[l]="";
          }
        int i=0;
        while (st.hasMoreTokens())
          {
            s2 = st.nextToken();
            a[i]=s2;
            i++;
            numTokens++;
          }
    
         int numTokens1 = 0;
         StringTokenizer st1 = new StringTokenizer (s3);
         String[] b = new String[10000];
         for(int k=0;k<10000;k++)
    
          {
             b[k]="";
          }
         int j=0;
         while (st1.hasMoreTokens())
           {
            s4 = st1.nextToken();
            b[j]=s4;
            j++;
            numTokens1++;
           }
    
    //comparing the contents of the files and printing the differences, if any.
      int x=0;
          for(int m=0;m<a.length;m++)
          {
       if(a[m].equals(b[m])){}
        else
        {
         x++;
      System.out.println(a[m] + " -- " +b[m]);
      System.out.println();}
          }
     System.out.println("No. of differences : " + x);
     if(x>0){System.out.println("Files are not equal");}
     else{System.out.println("Files are equal. No difference found");}
    
     }
    }