我試圖比較兩個.txt文件(它們的內容),但是當我執行此代碼時,進入一個時間循環。我想逐行比較。例如,在first.txt中,我有4行,在second.txt中有10行。我想比較這個(從first.txt):0 111 1
與0 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個文件,並指出你想要什麼?因爲從你的問題 – ProgrammingIsAwsome
不是很清楚,我在我的問題中加入了。現在更清楚了嗎? – user3698011
那麼現在發生了什麼?你知道每次哪個文件是'更長'的文件嗎?因此,如果A(20行)的所有行與文件B的前20行完全相同,那麼您說它們是相同的?你的意思是一次循環;)? – ProgrammingIsAwsome