我試圖打開兩個文本文件並在其中找到類似的行。 我的代碼正確讀取了兩個文本文件中的所有行。 我已經使用了嵌套的for
循環來比較第一個文本文件的line1
與第二個文本文件的所有行等等。 然而,它僅檢測具有相同行號類似的路線, (例如線的txt1
1是cc cc cc
和線的txt2
1是cc cc cc
,那麼它正確地找到並打印出來), 但它不檢測同一這些文件中的行號不同。如何在兩個文本文件中查找類似的行,而不考慮它們出現的行號
import java.io.*;
import java.util.*;
public class FeatureSelection500 {
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
File f1 = new File("E://implementation1/practise/ComUpdatusPS.exe.hex-04-ngrams-Freq.txt");
File f2 = new File("E://implementation1/practise/top-300features.txt");
Scanner scan1 = new Scanner(f1);
Scanner scan2 = new Scanner(f2);
int i = 1;
List<String> txtFileOne = new ArrayList<String>();
List<String> txtFileTwo = new ArrayList<String>();
while (scan1.hasNext()) {
txtFileOne.add(scan1.nextLine());
}
while (scan2.hasNext())
{
txtFileTwo.add(scan2.nextLine());
}
/*
for(String ot : txtFileTwo)
{
for (String outPut : txtFileOne)
{
// if (txtFileTwo.contains(outPut))
if(outPut.equals(ot))
{
System.out.print(i + " ");
System.out.println(outPut);
i++;
}
}
}
*/
for (int j = 0; j < txtFileTwo.size(); j++) {
String fsl = txtFileTwo.get(j);
// System.out.println(fileContentSingleLine);
for (int z = 0; z < 600; z++) // z < txtFileOne.size()
{
String s = txtFileOne.get(z);
// System.out.println(fsl+"\t \t"+ s);
if (fsl.equals(s)) {
System.out.println(fsl + "\t \t" + s);
// my line
// System.out.println(fsl);
} else {
continue;
}
}
}
}
}
兩個循環運作良好,但我沒有得到什麼不順心的,如果僅檢測在同一行號,而不是類似的文字聲明我希望它的方式是 –
請修復您的格式。這是很難理解的。 – BalinKingOfMoria
下降繼續; – StealthSpoder