比较文件中的行

时间:2014-03-20 03:27:56

标签: java file

我正在尝试比较文件1和文件2.

文件1:

7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1
7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1
7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1
6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 1
5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 1

文件2:

5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 -1
7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1
6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 -1
7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1
7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

在两个文件中,每行中的最后一个元素是类标签。

我正在比较类标签是否相等。 即比较

的类标签
line1:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

line2:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

匹配

比较

line1:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1

line2:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1

不匹配

更新

我做的是

String line1;
String line2;
int notequalcnt = 0;
while((line1 = bfpart.readLine())!=null){
found = false;
while((line2 = bfin.readLine())!=null){               
     if(line1.equals(line2)){
          found = true;
      break;
     }
   else{
    System.out.println("not equal");
    notequalcnt++;
   }
}

}

但我得到的每一个都是not equal

我做错了什么。

4 个答案:

答案 0 :(得分:2)

在第一次迭代后,line2变为null。因此,循环不会再次执行...在第一个while循环之后声明line2缓冲区。使用此代码:

public class CompareFile {

  public static void main(String args[]) throws IOException{

   String line1;
   String line2;
   boolean found;
   int notequalcnt =0;

   BufferedReader bfpart = new BufferedReader(new FileReader("file1.txt"));

   while((line1 = bfpart.readLine())!=null){
    found = false;
    BufferedReader bfin = new BufferedReader(new FileReader("file2.txt"));
    while((line2 = bfin.readLine())!=null){

        System.out.println("line1"+line1);
        System.out.println("line2"+line1);
        if(line1.equals(line2)){
            System.out.println("equal");

            found = true;
            break;

        }
        else{
            System.out.println("not equal");

        }
    }
    bfin.close();

    if(found==false)
      notequalcnt++;
    }
    bfpart.close();
  }
}

答案 1 :(得分:1)

您将文件1中的每一行与文件2中的每一行进行比较,并且您正在打印"不等于"每次他们中的任何一个都不匹配。

如果文件2有6行,并且你正在寻找文件1中的给定行(说它也在文件2中),则文件2中的5行不匹配,并且"不等于"将输出5次。

您当前的实施说明"如果文件2中的任何行不匹配,那么它不匹配",但您的真正含义是& #34;如果文件2 中的任何匹配,则 匹配"。所以你的逻辑(伪代码)应该更像这样:

for each line in file 1 {
   found = false
   reset file 2 to beginning
   for each line in file 2
      if line 1 equals line 2
          found = true, break.
   if found
      "found!"
   else
      "not found!"
}

此外,您将此描述为将文件1的第n行与文件2"的第n行进行比较,但实际上并不是您的实现所做的。您的实现实际上是将文件1的第一行与文件2的每一行进行比较然后停止,因为您已经在该内循环中消耗了文件2的每一行。

你的代码有很多问题,你可能需要先坐下来在纸上编写你的逻辑。

答案 2 :(得分:0)

如果目标是比较并找到匹配的行。将文件内容转换为arraylist并比较值。

Scanner s = new Scanner(new File("file1.txt"));
ArrayList<String> file1_list = new ArrayList<String>();
while (s.hasNext()){
    file1_list .add(s.next());
}
s.close();

s = new Scanner(new File("file2.txt"));
ArrayList<String> file2_list = new ArrayList<String>();
while (s.hasNext()){
    file2_list .add(s.next());
}
s.close();

for(String line1 : file1_list ){
    if(file2_list.contains(line1)){
       // found the line
    }else{
        // NOt found the line
    }
}

答案 3 :(得分:0)