2014-01-31 88 views
0

我想比较第一个文件的每一行与第二个文件的全部内容并打印匹配结果。 我使用这段代码,但它来到其他部分,并没有给我结果。 我的文件都是这样将第一个文件的每行与java中第二个文件的全部内容进行比较

file1 - store.txt    file2 - data.txt  

1,CNB,1234      1001,1234 
2,NCD,1567      1002,1345 
3,ABC,1111      1003,1111 
etc etc       etc etc 

我想要的结果是

CNB, 1234 
ABC, 1111 

这是我写的代码:

String y = ",", z = ";"; 

     File file1 = new File("store.txt"); 
     File file2 = new File("data.txt"); 

     BufferedReader bfr = new BufferedReader(new FileReader(file1)); 
     BufferedReader bfr1 = new BufferedReader(new FileReader(file2)); 

     String name1; 
     String name2; 
     String[] f1, f2; 

     try{ 

     while (bfr1.readLine() != null && bfr.readLine() != null) 
     { 

      name1 = bfr.readLine(); 
      name2 = bfr1.readLine(); 

      f1 = name1.split(y); 
      f2 = name2.split(z); 


      if (f1[1].equals(f2[1])) 
      { 
       //System.out.println(f1[0] + f1[1]); 
       for(int i=0; i < 1000;i++) { 
        name1 = bfr.readLine(); 

       name2 = bfr1.readLine(); 

       System.out.println(f1[0] + " \t here"); 
       } 

       //System.out.println(f1[0] + " \t here"); 
       // System.out.println(s1); 
      } 

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

    } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
} 
} 

感谢大家的帮助! :)

+0

这是怎么回事?..你的输出是什么? – TheLostMind

+0

o/p后面是什么逻辑? –

+0

'while(bfr1.readLine()!= null && bfr.readLine()!= null)'在循环的每次迭代中读取_and discards_一行。您只查看两个文件中的每一行。此外,您的程序中的逻辑是无法解读的。 –

回答

0

您的主要问题是,您从每个文件读取次数加载。你在while循环中阅读,然后扔掉它并再次阅读。另外,每次从第一个文件中读取时,您还会从第二个文件中读取。

您想从第二个文件读取一次并记住结果。

您想从循环中每次读取第一个文件一次。

例如:

name1 = bfr.readLine(); 
name2 = bfr2.readLine(); 

while (name1 != null) { 

    // Do stuff with name1 and name2 

    name1 = bfr.readLine(); 
} 
0

你如果条件应该是这样的

//f1[2] = 1234 and f2[1] = 1234 
    if (f1[2].equals(f2[1])) 
    { 
    // Write your logic here 
    } 
1

您的代码可以被优化了很多,因为其他海报所说,你的嵌套迭代没有保留的一首曲目迭代。您需要的“行取数”的数量会随着行数成指数增长,这实际上并不是很好。

还有一个逻辑错误因为你得到错误的这个比较f1[1].equals(f2[1]);从您预期的结果我想你想比较f1[2].equals(f2[1])

这里的好消息是,你可以使用两个包含HashMap存储两个重复的结果,然后就在它们之间迭代一次得到您的结果。

下面是代码,我没有测试它,但它应该工作:

private void compareFiles() { 
     BufferedReader bfr1 = null; 
     BufferedReader bfr = null; 
     String split = ","; 
     HashMap<String, String> fileCache1 = new HashMap<String, String>(); 
     HashMap<String, String> fileCache2 = new HashMap<String, String>(); 
     try { 
      File file1 = new File("store.txt"); 
      File file2 = new File("data.txt"); 
      bfr = new BufferedReader(new FileReader(file1)); 
      bfr1 = new BufferedReader(new FileReader(file2)); 

      String name1; 
      String name2; 
      String[] f1; 
      String[] f2; 
      try { 
       // read the first file and store it in an Hashmap (first column ignored) 
       while (bfr.readLine() != null) { 
        name1 = bfr.readLine(); 
        f1 = name1.split(split); 
        fileCache1.put(f1[2], f1[1]); 
       } 
       // read the second file and store it in an Hashmap (second column useless?) 
       while (bfr1.readLine() != null) { 
        name2 = bfr1.readLine(); 
        f2 = name2.split(split); 
        fileCache2.put(f2[1], f2[0]); // f2[0] is useless i think 
       } 

       // iterate once over the first hashmap and compare with the second. 
       for (String key1 : fileCache1.values()){ 
        if(fileCache2.containsKey(key1)){ // here is your desired match 
         System.out.println(fileCache1.get(key1)+", "+key1); // print or do whatever you want 
        } 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } catch (FileNotFoundException ex) { 
      Logger.getLogger(SO.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      try { 
       bfr1.close(); 
      } catch (IOException ex) { 
       Logger.getLogger(SO.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
+0

非常感谢你!但它不按我想要的方式工作!:/ – user2340345

+0

然后尝试更好地解释你想达到的目标。你只是把预期的结果,而不解释它背后的逻辑。我的逻辑实现基于一个假设,但它可以很容易地改变。真正的重点是创建两个hashmaps,然后比较。您将扫描一次这些文件,然后只是遍历驻留在内存中的散列表。 – elbuild

相关问题