-3

I want to compare a 2 or more Text Files to find a duplicate entry. O/P should say those lines in files are matched or not.

I want to compare the each of the File 1 lines with all lines of the File 2 (ie., Comparing File 1's line-1 with all lines of File 2). When I run the below code the that compares the line 1 of File 1 with all lines of File 2, then program got terminated.

Note: I tried Danail Alexiev's idea (See the answer) but the loop is running infinitely , (also not jumped to 2 line of File 1, infinite loop on File 1's line 1 with all lines of File 2)

Files Below

File 1: Content

21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s 
df0 
sd4f 
sdf 
sdf1 
3sdf 
sdfs4df6s 
fs1df 
3sdfsd 
fs.d1f 
s3d1
sdf1s 
df1 
sdf1sdf

File 2: Content

21321sc231231a23d1a32df1adfsdfsdfsd
fsdfs4dfs
dfsdf
3sd1f
sdfs4df3s 
df0 
sd4f 
sdf 
sdf1 
3sdf 
sdfs4df6s 
fs1df 
3sdfsd 
fs.d1f 
s3d1
sdf1s 
df1 
sdf1sdf

Code:

while ((sCurrentLine1 =file1.readLine()) != null )
 {
  while ((sCurrentLine2 =file2.readLine()) != null )
   {
    if(sCurrentLine1.equalsIgnoreCase(sCurrentLine2))
     {
      System.out.println("=---Matched----=" + sCurrentLine1 + " -->" + sCurrentLine2);
     }
     else
     {
      System.out.println("=---Not Matched----=" + sCurrentLine1 + " -->" + sCurrentLine2);
     }
    }
  }

O/P :

=---Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->1321sc231231a23d1a32df1adfsdfsdfsd =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fsdfs4dfs =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->dfsdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sd1f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdfs4df3s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->df0 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sd4f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sdf =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdfs4df6s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fs1df =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->3sdfsd =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->fs.d1f =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->s3d1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1s =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->df1 =---Not Matched----=1321sc231231a23d1a32df1adfsdfsdfsd -->sdf1sdf

  • 1
    So, you read the first line from the first file, then compare it to *every line* in the second file. Then you move on to the second line of the first file and compare it to...what? You've already reached the end of the second file. – JonK Jul 04 '16 at 14:00
  • You are comparing first line of file1 with every line of file2. Naturally, the first comparison returns 'Matched' when the others - don't. – dty Jul 04 '16 at 14:00
  • Please update your question with examples. Provide a simple example for both File1 and File2 and what you expect to happen during the comparisons. I think we're all having a hard time understanding what you are trying to accomplish – JeffC Jul 04 '16 at 19:04
  • updated the Question – Praveen Raja Jul 05 '16 at 20:12

2 Answers2

1

You are comparing each line of the second file to each line of the first file.

In order to do the right comparison, you have to check matching line numbers.

Change you while loop to:

while (((sCurrentLine1 = file1.readLine()) != null) && ((sCurrentLine2 = file2.readLine()) != null) {
    // your comparison
}

Be sure to include a check to detect the case where the files have a different number of lines.

EDIT:

After a clarification from OP, I think I know the problem.

You are trying to read the files as you go. When you compare the first line from File1 with all the lines from File2, you nested while loop will stop, because you have already read all lines from the file and readLine() will return null each time.

To fix this, you need to read all lines from File2 in advance, and use them to compare against the lines of File1.

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
  • Thanks for your reply, But I'm trying to check the each line of FILE1, with FILE2 lines. I tried above code, that will compare each line of Files like (line 1--> line1, line 2--> line2) But I want to compare -- line1 of file1 with all lines of file 2, line 2 of file1 with all lines of file2..etc... – Praveen Raja Jul 04 '16 at 14:19
  • Then you are getting the right result. Line 1 is clearly not matching any other line in file 2. What exactly is your problem? – Danail Alexiev Jul 04 '16 at 14:24
  • Mmm.... I'm going to compare 2 diff files , which will have 2 diff set of texts. So I want to compare the text files are having any duplicates Consider file have 10 lines and file 2 have 20 lines(**Like file 1 second line is repeated in file 2 15th line or 16th line** ) , when I tried its chking correctly for the first line (chking all lines for duplicates) but stops the loop without checking other lines in File 1 with file 2 lines – Praveen Raja Jul 04 '16 at 14:30
0
public void CompareFileMain()
{
   /*int tempvar = 0;
   int tempvar1 =0 ;*/
   String Tlines1[] = new String [100];
   String Tlines2[] = new String [100];
   String tempstring1 = null;
   String tempstring2 = null;
   int file1lines;
   int file2lines ;
   System.out.println(file1lines = linesOffile1());
   System.out.println(file2lines = linesOffile2());
   try {
    file1 = new BufferedReader(new FileReader(SOAPFile));
    file2 = new BufferedReader(new FileReader(RestFile));
    int oplines = 1;
    while ((sCurrentLine1 =file1.readLine()) != null ) 
    {
        tempstring1 = sCurrentLine1;
        while ((sCurrentLine2 =file2.readLine()) != null )
        {
               tempstring2 = sCurrentLine2;
                }
        if ((sCurrentLine2 =file2.readLine()) == null)
        {
           System.out.println("File 1 --> line# "+oplines +"\t");
           CompareFileSub(tempstring1, tempstring2);
        }
        oplines = oplines+1;
    }
        } catch (IOException e) {
        e.printStackTrace();
        }
}
public void CompareFileSub(String t1, String t2) throws IOException  // reading file 2 
{
  file2 = new BufferedReader(new FileReader(RestFile));
  int oplines = 1;
  while ((sCurrentLine2 =file2.readLine()) != null )
  {
    t2 = sCurrentLine2;
    if((t1.contains(t2) && (t2.contains(t1))))
    {
          System.out.print(t1+"-->"+t2+"---->matched \t");
          System.out.println("File 2 --> line# "+oplines +"\n");
    }
    else
    {
          System.out.print(t1+"-->"+t2+"---->not matched \t");
          System.out.println("File 2 --> line# "+oplines +"\n");
    }
              oplines = oplines+1;
 }
}

public int linesOffile1() //Count file 1 lines
{
  try {
    while ((sCurrentLine1 =file1.readLine()) != null )
    {
        //System.out.println("Taken line sCurrentLine1 "+sCurrentLine1);
        //System.out.println("Taken line sCurrentLine1 "+i);
        i=i+1;
    }                                   
        } catch (IOException e) {
            e.printStackTrace();
            }
   finally {
    try {
           if (file1 == null)file1.close();
         } catch (IOException ex) {
                ex.printStackTrace();
                }
    }
   return i;

}      
public int linesOffile2() //Count file 2 lines
{
  try {
    while ((sCurrentLine2 =file2.readLine()) != null )
    {   
    //System.out.println("Taken line sCurrentLine2 "+sCurrentLine2);
    //System.out.println("Taken line sCurrentLine2 "+j);
    j=j+1;
    }
        } catch (IOException e) {
            e.printStackTrace();
            }   
  finally {
    try {
        if (file2 == null)file1.close();
    } catch (IOException ex) {
            ex.printStackTrace();
            }
    }
 return j;
}      

InPut:

File1:

java
c++

test
stack
test
overflow
extraLine

File2:

stack
overflow
test
java
c++

test

Ouput

8
7
File 1 --> line# 1  
java-->stack---->not matched    File 2 --> line# 1

java-->overflow---->not matched     File 2 --> line# 2

java-->test---->not matched     File 2 --> line# 3

java-->java---->matched     File 2 --> line# 4

java-->c++---->not matched  File 2 --> line# 5

java-->---->not matched     File 2 --> line# 6

java-->test---->not matched     File 2 --> line# 7

File 1 --> line# 2  
c++-->stack---->not matched     File 2 --> line# 1

c++-->overflow---->not matched  File 2 --> line# 2

c++-->test---->not matched  File 2 --> line# 3

c++-->java---->not matched  File 2 --> line# 4

c++-->c++---->matched   File 2 --> line# 5

c++-->---->not matched  File 2 --> line# 6

c++-->test---->not matched  File 2 --> line# 7

File 1 --> line# 3  
-->stack---->not matched    File 2 --> line# 1

-->overflow---->not matched     File 2 --> line# 2

-->test---->not matched     File 2 --> line# 3

-->java---->not matched     File 2 --> line# 4

-->c++---->not matched  File 2 --> line# 5

-->---->matched     File 2 --> line# 6

-->test---->not matched     File 2 --> line# 7

File 1 --> line# 4  
test-->stack---->not matched    File 2 --> line# 1

test-->overflow---->not matched     File 2 --> line# 2

test-->test---->matched     File 2 --> line# 3

test-->java---->not matched     File 2 --> line# 4

test-->c++---->not matched  File 2 --> line# 5

test-->---->not matched     File 2 --> line# 6

test-->test---->matched     File 2 --> line# 7

File 1 --> line# 5  
stack-->stack---->matched   File 2 --> line# 1

stack-->overflow---->not matched    File 2 --> line# 2

stack-->test---->not matched    File 2 --> line# 3

stack-->java---->not matched    File 2 --> line# 4

stack-->c++---->not matched     File 2 --> line# 5

stack-->---->not matched    File 2 --> line# 6

stack-->test---->not matched    File 2 --> line# 7

File 1 --> line# 6  
test-->stack---->not matched    File 2 --> line# 1

test-->overflow---->not matched     File 2 --> line# 2

test-->test---->matched     File 2 --> line# 3

test-->java---->not matched     File 2 --> line# 4

test-->c++---->not matched  File 2 --> line# 5

test-->---->not matched     File 2 --> line# 6

test-->test---->matched     File 2 --> line# 7

File 1 --> line# 7  
overflow-->stack---->not matched    File 2 --> line# 1

overflow-->overflow---->matched     File 2 --> line# 2

overflow-->test---->not matched     File 2 --> line# 3

overflow-->java---->not matched     File 2 --> line# 4

overflow-->c++---->not matched  File 2 --> line# 5

overflow-->---->not matched     File 2 --> line# 6

overflow-->test---->not matched     File 2 --> line# 7

File 1 --> line# 8  
extraLine-->stack---->not matched   File 2 --> line# 1

extraLine-->overflow---->not matched    File 2 --> line# 2

extraLine-->test---->not matched    File 2 --> line# 3

extraLine-->java---->not matched    File 2 --> line# 4

extraLine-->c++---->not matched     File 2 --> line# 5

extraLine-->---->not matched    File 2 --> line# 6

extraLine-->test---->not matched    File 2 --> line# 7

Got the solution, thank you all