繁体   English   中英

比较两个文件中的值

[英]Compare values in two files

我有两个文件,它们应该在子串0和10之间包含相同的值但不按顺序排列。 我有管理来打印每个文件中的值,但我需要知道如何报告说id,值在第一个文件中,而在第二个文件中,反之亦然。 文件采用这些格式。

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的新手,可能不会轻易抓住这些想法,但我正在尝试。

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

 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;
}

也许你正在寻找这样的东西

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);

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

  • 将每个文件的值相应地放到两个单独的HashSet
  • 迭代其中一个HashSet并检查另一个HashSet是否存在每个值。 如果没有报告。
  • 迭代其他HashSet并为此做同样的事情。

打开两个扫描仪,然后:

    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修改编辑

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

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

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

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

class Element {
  public String str;
  public int lineNr;

  public boolean equals(Element compElement) {
    return compElement.str.equals(str);
  }
}

然后你可以改用Set<Element>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM