2016-07-04 24 views
-3

我想比較2個或更多文本文件來查找重複條目。 O/P應該說這些文件中的行匹配或不匹配。用於2個或更多文本文件比較的JAVA程序

我想用文件的所有行,比較各文件1條線2(即,文件1的行1比較文件2中的所有行)。 當我運行下面的代碼時,比較文件1的第1行和文件2的所有行,然後程序終止。

注意:我試過Danail阿列克謝耶夫的想法(一看便知),但循環無限運行,(還沒有上升到2行文件1,在文件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

= --- ----匹配= 1321sc231231a23d1a32df1adfsdfsdfsd - > 1321sc231231a23d1a32df1adfsdfsdfsd = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > fsdfs4dfs = ---不匹配---- = 1321sc231231a23d1a32df1adf sdfsdfsd - > dfsdf = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sd1f = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdfs4df3s = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > DF0 = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sd4f = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > SDF = ---不匹配--- - = 1321sc231231a23d1a32df1adfsdfsdfsd - > SDF1 = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sdf = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdfs4df6s = ---未Matched-- - = 1321sc231231a23d1a32df1adfsdfsdfsd - > fs1df = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > 3sdfsd = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > fs.d1f = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > s3d1 = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - - > sdf1s = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > DF1 = ---不匹配---- = 1321sc231231a23d1a32df1adfsdfsdfsd - > sdf1sdf

+1

所以,你讀的第一個文件的第一行,然後把它比作*每一行*在第二個文件。然後你移動到第一個文件的第二行,並將它與......進行比較?您已經到達第二個文件的末尾。 – JonK

+0

您正在比較file1的第一行和file2的每一行。當然,第一個比較返回「匹配」時,其他人 - 不。 – dty

+0

請用例子更新你的問題。爲File1和File2提供一個簡單示例,以及在比較過程中您期望發生的情況。我認爲我們都很難理解你要完成什麼 – JeffC

回答

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

輸入:

File1中:

java 
c++ 

test 
stack 
test 
overflow 
extraLine 

文件2:

stack 
overflow 
test 
java 
c++ 

test 

輸出繼電器

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 

得到了解決,謝謝大家

1

您正在比較的每一行第二個文件分配給第一個文件的每一行。

爲了做正確的比較,你必須檢查匹配的行號。

更改您while循環:

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

一定要包括檢查,以檢測其中的文件有不同的行數的情況。

編輯:

從OP澄清之後,我想我知道這個問題。

您正在嘗試讀取文件。當比較File1的第一行和File2的所有行時,嵌套的while循環將停止,因爲您已經讀取了文件中的所有行,並且readLine()每次都會返回null

要解決此問題,需要事先讀取File2中的所有行,然後使用它們與File1的行進行比較。

+0

感謝您的回覆, 但我試圖用FILE2行檢查FILE1的每一行。我試過上面的代碼,它會比較每行文件,如(第1行 - >第1行,第2行 - >第2行) 但我想比較 - file1的line1與文件2的所有行,第2行file1與file2..etc的所有行... –

+0

然後,你會得到正確的結果。第1行顯然不匹配文件2中的任何其他行。究竟是什麼問題? –

+0

嗯....我要比較2個差異文件,其中將有2個差異文本集。所以我想比較文本文件是否有任何重複的文件 考慮文件有10行和文件2有20行(**像文件1第二行重複在文件2第15行或第16行**),當我嘗試它爲第一行正確撥號(扼殺所有行重複),但停止循環,而不檢查文件1中的其他行與文件2行 –