Compare CSV Files in Java

We have already discussed the basic way how to compare two CSV files within our Java applications. Today, in this article, we will further learn to compare more than two CSV in Java. We will further discuss how to compare password-protected CSV files. Lastly, will look into how to accept and reject the found changes using the comparison API within the Java application.

The following topics are covered below:

Java API for Comparing CSV Files

GroupDocs.Comparison showcases its Java API that allows comparing CSV files with different options. I will again use it to compare CSV files within the Java application. Additionally, it supports the comparison of more than two CSV files, accepting and rejecting the discovered changes, and comparing password-protected files.

Download and Configure

Get the CSV comparison library from the downloads section. For Maven-based Java applications, add the following configuration within pom.xml.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-comparison</artifactId>
        <version>22.3</version> 
</dependency>

Running examples for its features are also available on GitHub. Visit its documentation and API Reference for guidance.

Compare CSV Files in Java

The basic comparison of two CSV files using Java has already been discussed in a separate article. There, we looked into the file comparison Java source code, sample input, and the comparison output file. You can visit that article before you move forward.

Compare More than Two CSV Files in Java

If the requirement is to compare more than two CSV files, it is quite similar as we are comparing only two files. Just add the third file to the Comparer object.

The following are the steps to compare more than two CSV files in Java.

  • Instantiate the Comparer class with the first CSV file.
  • Add the second file using the appropriate add method.
  • Similarly, add as many other CSV files using the add methods.
  • Finally, call the compare method the get the comparison results.

The following Java source code compares multiple CSV files. As the comparison results, it generates separate CSV and HTML files. The HTML output file highlights the identified changes in different colors.

Compare Password Protected CSV Files in Java

There are different available options that can be used while loading different documents. If your CSV files are password-protected, you can provide the password while loading. The rest of the procedure remains the same for comparing multiple password-protected CSV files.

The following are the steps for how to compare password-protected CSV files in Java.

  • Prepare the loading options using the LoadOptions class and set the password to open the CSV file.
  • Now load the first CSV file to compare while initializing the Comparer class.
  • Add the second file using the appropriate add method.
  • Similarly, add as many other CSV files using any of the add methods.
  • Finally, invoke the compare method to get the comparison result of all the added files.

The following source code compares multiple password-protected CSV files in Java and generates the comparison results.

Accept or Reject Identified Changes of CSV Files in Java

Just like the track changes feature of Microsoft Word that allows us to accept or reject the made changes within the documents. You can implement the same feature within the Java applications. After performing the file comparison, you can accept or reject each of the identified changes. The following are the steps to compare CSV files and accept or reject the identified changes.

  • Load the source and as well as target CSV files using the Comparer class.
  • Perform the comparison of the loaded files using the compare method.
  • Fetch the identified changes using the getChanges method.
  • Now you can traverse the changes and set their ComparisonAction accordingly for each change.
    • For the agreed changes you can set it as ACCEPT.
    • For the disagreed changes you can set it as REJECT.
  • Finally, call the applyChanges method to get the resultant document having the applied changes.

The following Java source code compares two CSV files and then accepts and rejects the found changes.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, today we learned the Java way how to compare two or more CSV files. Along with the option of accepting and rejecting the identified changes, we also learned to compare password-protected CSV files within the Java applications.

You can find many other options to customize the comparison results. You can set the comparison sensitivity, show only the summary page, ignore gaps, and many more. Learn about these features from the documentation.

You can start building your own comparison applications for various supported document formats. For queries, contact us via the forum.

See Also