【问题标题】:Compare values in two files比较两个文件中的值
【发布时间】:2012-07-09 11:50:13
【问题描述】:

我有两个文件,它们应该在子字符串 0 和 10 之间包含相同的值,但不是按顺序排列的。我已经设法输出每个文件中的值,但我需要知道如何报告说值在第一个文件中和第二个文件中的值,反之亦然。文件采用这些格式。

6436346346....Other details
9348734873....Other details
9349839829....Other details

第二个文件

8484545487....Other details
9348734873....Other details
9349839829....Other details

第一个文件中的第一条记录没有出现在第二个文件中,第二个文件中的第一条记录也没有出现在第一个文件中。我需要能够以这种格式报告这种不匹配:

Record 6436346346 is in the firstfile and not in the secondfile.
Record 8484545487 is in the secondfile and not in the firstfile.

这是我目前拥有的代码,它为我提供了两个文件所需的输出以进行比较。

package compare.numbers;

import java.io.*;

/**
 *
 * @author implvcb
 */
 public class CompareNumbers {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
    // TODO code application logic here
    File f = new File("C:/Analysis/");
    String line;
    String line1;
    try {
        String firstfile = "C:/Analysis/RL001.TXT";
        FileInputStream fs = new FileInputStream(firstfile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        while ((line = br.readLine()) != null) {
            String account = line.substring(0, 10);
             System.out.println(account);


        }
        String secondfile = "C:/Analysis/RL003.TXT";
        FileInputStream fs1 = new FileInputStream(secondfile);
        BufferedReader br1 = new BufferedReader(new InputStreamReader(fs1));
        while ((line1 = br1.readLine()) != null) {
            String account1 = line1.substring(0, 10);
            System.out.println(account1);
        }

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



}
}

请帮助我如何有效地实现这一目标。 我想我需要说我是 Java 新手,可能不会轻易抓住这些想法,但我正在尝试。

【问题讨论】:

    标签: java bufferedreader


    【解决方案1】:

    这是执行此操作的示例代码:

     public static void eliminateCommon(String file1, String file2) throws IOException
    {
        List<String> lines1 = readLines(file1);
        List<String> lines2 = readLines(file2);
    
        Iterator<String> linesItr = lines1.iterator();
        while (linesItr.hasNext()) {
            String checkLine = linesItr.next();
            if (lines2.contains(checkLine)) {
                linesItr.remove();
                lines2.remove(checkLine);
            }
        }
    
        //now lines1 will contain string that are not present in lines2
        //now lines2 will contain string that are not present in lines1
        System.out.println(lines1);
        System.out.println(lines2);
    
    }
    
    public static List<String> readLines(String fileName) throws IOException
    {
        List<String> lines = new ArrayList<String>();
        FileInputStream fs = new FileInputStream(fileName);
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        String line = null;
        while ((line = br.readLine()) != null) {
            String account = line.substring(0, 10);
            lines.add(account);
        }
        return lines;
    }
    

    【讨论】:

    • 使用集合会更有效地进行搜索。
    • @Stanley 在 EliminationCommon() 结束时,您有 cmets。这两个列表都包含唯一的 ID。您可以按照自己的方式打印。
    • System.out.println(lines1)System.out.println(lines2) 没有打印任何内容。
    • 谢谢拉梅什,我想这就是我一直在寻找的东西,最后一件事,输出是一个数组,格式为:[2632323236, 734343476, 34734343834],我得到了每个数字在自己的线路上?
    • lines1 不返回行 2 中的记录,但 linees2 甚至返回第一个文件中的记录
    【解决方案2】:

    也许你正在寻找类似的东西

    Set<String> set1 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL001.TXT")));
    Set<String> set2 = new HashSet<>(FileUtils.readLines(new File("C:/Analysis/RL003.TXT")));
    
    Set<String> onlyInSet1 = new HashSet<>(set1);
    onlyInSet1.removeAll(set2);
    
    Set<String> onlyInSet2 = new HashSet<>(set2);
    onlyInSet2.removeAll(set1);
    

    【讨论】:

    • 那么什么参数代表文件 1 中的内容而不是文件中的内容以及文件 2 中的内容而不是文件 1 中的内容?
    • 遇到错误:源 1.6 中不支持菱形分隔符
    • onlyInSet1 = "文件 1 中的内容和文件中没有的内容" 2
    • 可以填写&lt;String&gt;类型。 ;)
    【解决方案3】:

    如果您保证文件格式始终相同,并且每个 readLine() 函数将返回不同的数字,为什么不使用字符串数组而不是单个字符串。然后,您可以更轻松地比较结果。

    【讨论】:

      【解决方案4】:

      好的,首先我将两组字符串保存到集合中

      Set<String> s1 = new HashSet<String>(), s2 = new HashSet<String>();
      //...
      while ((line = br.readLine()) != null) {
        //...
        s1.add(line);
      }
      

      然后您可以比较这些集合并找到两个集合中都没有出现的元素。你可以找到一些关于如何做到这一点的想法here

      如果您还需要知道行号,您可以创建一个字符串包装器:

      class Element {
        public String str;
        public int lineNr;
      
        public boolean equals(Element compElement) {
          return compElement.str.equals(str);
        }
      }
      

      那你就可以改用Set&lt;Element&gt;了。

      【讨论】:

        【解决方案5】:

        打开两个扫描仪,然后:

            final TreeSet<Integer> ts1 = new TreeSet<Integer>();    
            final TreeSet<Integer> ts2 = new TreeSet<Integer>();
            while (scan1.hasNextLine() && scan2.hasNexLine) {
                    ts1.add(Integer.valueOf(scan1.nextLigne().subString(0,10));
                    ts1.add(Integer.valueOf(scan1.nextLigne().subString(0,10));
                }
        You can now compare ordered results of the two trees
        

        编辑 用 TreeSet 修改

        【讨论】:

        • 数字是否按排序顺序(递增或递减)给出?如果不能保证这一点,则此解决方案将不起作用。 (未排序问题中的示例)
        • 不,数字是随机的,没有输出数字的顺序。
        • OP 表示值不按顺序排列。
        【解决方案6】:
        • 将每个文件中的值相应地放入两个单独的HashSets。
        • 遍历HashSets 之一并检查每个值是否存在于另一个HashSet 中。如果没有,请报告。
        • 遍历其他 HashSet 并为此做同样的事情。

        【讨论】:

        • 更简单:hashset1.removeAll(hashset2)。剩下的所有元素都是单一的。然后在另一个方向做同样的事情(当然是新的集合)。
        猜你喜欢
        • 1970-01-01
        • 2018-11-08
        • 2023-02-04
        • 1970-01-01
        • 2014-11-11
        相关资源
        最近更新 更多