Saturday, May 7, 2022

Compare two CSV files and find common values

 Compare two CSV files and find common values or print ?

Question : We have two csv files like firstName.csv and lastName.csv. firstName.csv have one name in one row and lastName.csv have one lastName in one row.

We need to find the common values in both file and print. 

Answer: To compare two CSV files we have to follow below steps.

  • Read first CSV file and store in ArrayList.
  • Read second CSV file line by line and check in first csv file ArrayList.
  • if second file's data already exist in first file csv ArrayList than print value.
Image : firstName.csv
Image : lastName.csv

Let's see below Java Code:
package com.javaiq.in;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/*
 * Program to print common data from two csv files firstName.csv and lastName.csv
 */
public class PrintCommonDataCSV {
	public static void main(String... args) {

		// Get All data from firstName.csv to ArrayList
		List<String> firstNameList = new ArrayList<>();
		Path pathToFile = Paths.get("firstName.csv");
		System.out.println(" pathToFile :" + pathToFile);
		try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {
			String line = br.readLine();
			while (line != null) {
				firstNameList.add(line.trim());
				line = br.readLine();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		System.out.println(firstNameList);

		/*
		 * Check if LastName present in firstNameList that mean its common data to both
		 * files and print
		 */
		Path pathToFileLastName = Paths.get("lastName.csv");
		System.out.println(" pathToFileLastName :" + pathToFileLastName);
		try (BufferedReader br = Files.newBufferedReader(pathToFileLastName, StandardCharsets.US_ASCII)) {
			String lastName = br.readLine();
			while (lastName != null) {
				if (firstNameList.contains(lastName)) {
					System.out.println("Common data in firstName.csv and lastName.csv : " + lastName);
				}
				lastName = br.readLine();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
}
Output :
 pathToFile :firstName.csv
[Sanjeev, Kumar, Amit, Rahul, Gupta, Kamal]
 pathToFileLastName :lastName.csv
Common data in firstName.csv and lastName.csv : Kumar
Common data in firstName.csv and lastName.csv : Gupta
As you can see in output of above code, Kumar and Gupta are common data in both files.

No comments:

Post a Comment