CSV files data is basically comma-separated values and it is very difficult to read such data in raw format. However, when these files are viewed using any of the well-known editors like Microsoft Excel, OpenOffice Calc, or LibreOffice that support CSV format, it becomes quite easy to manipulate CSV files. Even then, what about if you want to compare two large CSV files for their difference in data? Let’s do it programmatically. This article quickly covers the basic way how to compare two CSV files in Java.

Java API for Comparing CSV Files

GroupDocs showcases its APIs that allow comparing two or more CSV files for differences within the applications. I will use its Java API i.e. GroupDocs.Comparison for Java to compare CSV files.

The API also supports the comparison of more than two CSV files, comparing password-protected CSV files, accepting and Rejecting the discovered changes, and much more.

Download and Configure

Get the CSV comparison library from the downloads section. For Maven-based Java applications, add the following configuration within pom.xml. Later, you can try the examples of this article as well as many more from GitHub. For the details, you may also visit the API Reference.

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

How to Compare CSV Files in Java

Straight to the topic, let’s do the basic comparison. It is as simple as any online CSV comparison application. Just load the comparison file and then hit compare. The following are the files that are compared below:

CSV Files to Compare

The following are the steps to compare two CSV files for differences in Java:

  • Load the first CSV file using the Comparer class.
  • Add the second CSV file using the respective add method.
  • Compare both the files using compare method.

The following Java code compares the CSV files and provides the differences in a CSV output.

The output result is as follows:

CSV Files Compaison Result

Conclusion

To sum up, we just learned to compare two comma-separated values CSV files within a Java application. In the summary of the comparison, we also get the count for differences found within the compared files. This feature lets you find the dissimilarities between two large CSV files. Similarly, you can build your own CSV files comparison Java application using this simple feature.

For more details and to learn about the API, visit its documentation. For queries, contact us via the forum.

See Also