How to find lines with the same fields in two text files in Java

Key words: txt comparison

Does java have any convenient class libraries for file comparison? For example, for the old.txt and new.txt files, the first line is the column name. We need to retrieve the same content of the name field in the files. Some data are as follows:

Old.txt

New.txt

Name   Dept

Rachel Sales

Ashley R&D

Matthew   Sales

Alexis Sales

Megan  Marketing

Name   Dept

Emily  HR

Ashley R&D

Matthew   Sales

Alexis Sales

Megan  Marketing

The expected result is as follows:

undefined

Java is not a set language. It needs a lot of loops and judgments, and the code will be long. The code should be written as follows:

...

while ((String lineText = comparedReader.readLine()) != null) {

  String comparedLine = lineText.trim();

  if (searchText.equals(comparedLine)) {

    lineStr = "###=Equal:" + lineNum +   "=###\n";

    break;

  }

  lineNum++;

}

rbw.write(lineStr);

comparedReader.reset();

...

In terms of text comparison, it would be much simpler to use esProc SPL. A set of function library in the field of set operation has been designed carefully to realize the same process with more succinct code. For example, the above problem only needs two lines:


A

1

=Old=file("Old.txt").import@t(),New=file("New.txt").import@t()

2

=Old.(Name)^ New.(Name)

In fact, there are many cases where it is not convenient to use java to process text, even for large file comparison, join calculation, importing into database and other requirements. However, it is very simple to use esProc SPL, please refer to: Structured Text Computing with esProc , Structured Text File Processing in SPL (II)

esProc is integration-friendly. Read How to Call an SPL Script in Java to see how we can easily embedded an SPL script into a Java program.

Read Getting Started with esProc to download and install esProc, get a license for free and find related documentation.