2015-01-12 128 views
0

我試圖比較兩個.txt文件(它們的內容),但是當我執行此代碼時,進入一個時間循環。我想逐行比較。例如,在first.txt中,我有4行,在second.txt中有10行。我想比較這個(從first.txt):0 111 10 111 1(second.txt)和0 112 1(second.txt)。如你所見,它們具有不同的價值,但具有相同的來源和目標。 所以,從first.txt我可以比較second.txt超過1次。在java中比較2個不同內容的文本文件(對於text1中的每一行,需要與text2中的每一行進行比較)

有人可以糾正我的錯誤嗎?

這是文件我的意思是: First.txt

source value target 
0 111  1 
0 222  2 
0 333  3 
0 444  4 

Second.txt

source value target 
0 111  1 
0 222  2 
0 333  3 
0 444  4 
1 555  1 
2 987  1 
0 112  1 
0 223  2 
0 334  3 
0 445  4 

這裏是我的代碼:

 // create new reader for cs 
     frCs = new FileReader("nodeCsArr.txt"); 
     lnrCs = new LineNumberReader(frCs); 

     // create new reader for rs 
     frRs = new FileReader("nodeRsArr.txt"); 
     lnRs = new LineNumberReader(frRs); 

     System.out.println("------Read distance Current State-----"); 
     // read distance on Cs 
     while((strCs=lnrCs.readLine())!=null){ 
      i=lnrCs.getLineNumber(); 
      //edited here 
      System.out.println("snCs baris ke-: "+i); 

      stateArray=strCs.split("\t"); 

      //kolomnya 
      snCs= stateArray[0]; System.out.println("snCs: "+snCs); 
      dCs= stateArray[1]; //System.out.println("dCs: "+dCs); 
      tnCs = stateArray[2]; 
      NodeLength=dCs.length(); System.out.println("panjang= "+NodeLength); 

      System.out.println("------Read distance Random State-----"); 
      // read distance on Rs 
      if((strRs=lnRs.readLine())!=null){ 
       i=lnRs.getLineNumber(); 
       //edited here 
       System.out.println("snRs baris ke-: "+i); 

       stateArray=strRs.split("\t"); 

       //input array--starts opencl here 
       snRs= stateArray[0]; System.out.println("snRs: "+snRs); 
       dRs= stateArray[1]; //System.out.println("dRs: "+dRs); 
       tnRs = stateArray[2]; 

       if ((snCs.equals(snRs))&&(tnCs.equals(tnRs))) { 
        System.out.println("snCs= "+snCs+" tnCs= "+tnCs+" dcs= "+dCs); 
        System.out.println("snRs= "+snRs+" tnRs= "+tnRs+" drs= "+dRs); 
        dTot=0; 
        for (int j = 0; j < NodeLength; j++) { 

         if (dCs.toCharArray()[j]==dRs.toCharArray()[j]) { 
          difBit=0;  
         }else{ 
          difBit=1; 
         } 
         dTot=dTot+difBit; 
        } 
        dist=dTot/NodeLength; 
        System.out.println("different bit= "+dTot); 
        System.out.println("distance= "+dist); 
       } 
      } 
     } 
     //lnRs.close(); 

     lnrCs.close();` 
+2

你能提供你的2個文件,並指出你想要什麼?因爲從你的問題 – ProgrammingIsAwsome

+0

不是很清楚,我在我的問題中加入了。現在更清楚了嗎? – user3698011

+0

那麼現在發生了什麼?你知道每次哪個文件是'更長'的文件嗎?因此,如果A(20行)的所有行與文件B的前20行完全相同,那麼您說它們是相同的?你的意思是一次循環;)? – ProgrammingIsAwsome

回答

0

這是我的解決方案。我只是在ArrayList中逐行讀取第一個文件(不管是「更長」還是「更短」)。然後,我將逐行讀取第二個文件並比較這些行。

first.txt:

0 111 
0 222 
0 333 
0 444 

second.txt:

0 111 
0 222 
0 333 
0 444 
1 555 
2 987 

這裏的代碼(simpicity只是在main方法:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class CompareFiles { 

    public static void main(String[] args) { 
     boolean same = true; 
     Scanner in = null; 

     try { 
      in = new Scanner(new FileReader("first.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     ArrayList<String> firstLines = new ArrayList<String>(); 
     while (in.hasNextLine()) { 
      firstLines.add(in.nextLine()); 
     } 
     in.close(); 

     try { 
      in = new Scanner(new FileReader("second.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     int counter = 0; 
     while (in.hasNextLine()) { 
      if (counter < firstLines.size()) { 
       if (!((in.nextLine()).equals(firstLines.get(counter)))) { 
        same = false; 
        break; 
       } 
       counter++; 
      } else { 
       break; 
      } 
     } 
     in.close(); 
     System.out.println(same); 
    } 
} 

記住,我只需檢查文件是否相同,從文件開頭開始。 So: 甲 甲 和 甲 乙 甲 甲 不會是相同的。這真的取決於你如何定義平等。

+0

謝謝你的幫助。但這不是我的意思。我再次編輯我的問題,可否請您閱讀它? – user3698011

0

我建議使用掃描器閱讀每一行,即:

import java.util.Scanner; 
(...) 
Scanner stdin = new Scanner(System.in); 
String line = stdin.nextLine(); 

比較他們在兩個循環(1。 A中的每一行,2)B中的每一行) - 從A獲得第一行並與每一條B行進行比較,直到到達lineBlength,然後獲得第二行frm A並與B的每一行進行比較,從A得到下一行。 ..

相關問題