Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java comparing-two-csv-files-in-java

import java.io.*;
import java.util.ArrayList;

/* file1 - file2 = file3*/
public class CompareCSV {
public static void main(String args[]) throws FileNotFoundException, IOException
{
    String path="D:csv";
    String file1="file1.csv";
    String file2="file2.csv";
    String file3="p3lang.csv";
    ArrayList al1=new ArrayList();
    ArrayList al2=new ArrayList();
    //ArrayList al3=new ArrayList();

    BufferedReader CSVFile1 = new BufferedReader(new FileReader(path+file1));
    String dataRow1 = CSVFile1.readLine();
    while (dataRow1 != null)
    {
        String[] dataArray1 = dataRow1.split(",");
        for (String item1:dataArray1)
        { 
           al1.add(item1);
        }

        dataRow1 = CSVFile1.readLine(); // Read next line of data.
    }

     CSVFile1.close();

    BufferedReader CSVFile2 = new BufferedReader(new FileReader(path+file2));
    String dataRow2 = CSVFile2.readLine();
    while (dataRow2 != null)
    {
        String[] dataArray2 = dataRow2.split(",");
        for (String item2:dataArray2)
        { 
           al2.add(item2);

        }
        dataRow2 = CSVFile2.readLine(); // Read next line of data.
    }
     CSVFile2.close();

     for(String bs:al2)
     {
         al1.remove(bs);
     }

     int size=al1.size();
     System.out.println(size);

     try
        {
            FileWriter writer=new FileWriter(path+file3);
            while(size!=0)
            {
                size--;
                writer.append(""+al1.get(size));
                writer.append('
');
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
}}
Comment

Compare two csv files using java

//download csv reader maven dependency for this
public class ReadCSV {
    public List<Map<String, String>> read(String filename) throws CsvValidationException, IOException {

        CSVReader csvReader = new CSVReader(new FileReader(filename));

        String[] headerArray = csvReader.readNext();
        String[] lineInArray;

        List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

        while ((lineInArray = csvReader.readNext()) != null) {
            HashMap<String, String> map = new HashMap<String, String>();

            for (int i = 0; i < lineInArray.length; i++) {
                map.put(headerArray[i], lineInArray[i]);
            }

            listOfMaps.add(map);
            //System.out.println(map);
        }

        return listOfMaps;

    }


    public static void main(String[] args) throws IOException, CsvException {
        
        //select relative path of csv
        
        String filename1 = "test1.csv";
        String filename2 = "test2.csv";
        

        ReadCSV readCSV = new ReadCSV();

        List<Map<String, String>> file1 = readCSV.read(filename1);
        List<Map<String, String>> file2 = readCSV.read(filename2);

        for (int i = 0; i < file1.size(); i++) {

            if (!file1.get(i).equals(file2.get(i))) {
                System.out.println("file1: " + file1.get(i));
                System.out.println("file2: " + file2.get(i));
                System.out.println("---------------------");
            }
        }


    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to use string variables with an if statement in java 
Java :: uninstall java 
Java :: Array List java can I add a pair of element 
Java :: arraylist vs vector in java 
Java :: Spigot coding how to send a message to player after commadn 
Java :: spigot plugin send message 
Java :: java sort method 
Java :: how to addin java 
Java :: how to convert 2d to 1d array in java 
Java :: java how to know if there is something on arguments 
Java :: write file java 
Java :: how to find armstrong numbers in java 
Java :: Java Thread Example by implementing Runnable interface 
Java :: java.lang.IllegalStateException: Could not obtain identifier 
Java :: how to find the length of an array in java 
Java :: initializing list in java 
Java :: jsp initialization 
Java :: kkkkkkkkkkkkkk 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: how to covert array into a char 
Java :: java compare lists 
Java :: how to create a subclass in java 
Java :: java scanner int to string 
Java :: How to initialize a 3d array in Java? 
Java :: difference between getcontext and getapplicationcontext in android 
Java :: Add an element to ArrayList using addall() method 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: java base64 decrypt script 
Java :: miles to km converter programm in java 
Java :: Java Creating a TreeSet 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =