JAVA 用于比较 2 个或更多文本文件的程序

JAVA program for 2 or more text files comparing

我想比较 2 个或更多文本文件以查找重复条目。 O/P 应该说文件中的那些行是否匹配。

我想将 File 1 的每一行与 File 2 的所有行进行比较(即比较文件 1 的第 1 行与文件 2 的所有行)。 当我 运行 下面的代码将文件 1 的第 1 行与文件 2 的所有行进行比较时,程序终止。

注意:我试过Danail Alexiev的想法(见答案)但是循环是运行ning无限, (也没有跳到文件 1 的第 2 行,文件 1 的第 1 行与文件 2 的所有行无限循环)

以下文件

文件 1:内容

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

文件 2:内容

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

代码:

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

您正在将第二个文件的每一行与第一个文件的每一行进行比较。

为了进行正确的比较,您必须检查匹配的行号。

将您的 while 循环更改为:

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

一定要包括检查以检测文件行数不同的情况。

编辑:

经过OP的澄清,我想我知道了问题所在。

您正在尝试边读边读文件。当您将 File1 的第一行与 File2 的所有行进行比较时,嵌套的 while 循环将停止,因为您已经从文件中读取了所有行并且 readLine() 将 return null 每个时间.

要解决此问题,您需要提前读取 File2 中的所有行,并使用它们与 File1 中的行进行比较。

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

解决了,谢谢大家