java comparing two csv files in java

Solutions on MaxInterview for java comparing two csv files in java by the best coders in the world

showing results for - "java comparing two csv files in java"
Fernando
12 Jan 2017
1import java.io.*;
2import java.util.ArrayList;
3
4/* file1 - file2 = file3*/
5public class CompareCSV {
6public static void main(String args[]) throws FileNotFoundException, IOException
7{
8    String path="D:\\csv\\";
9    String file1="file1.csv";
10    String file2="file2.csv";
11    String file3="p3lang.csv";
12    ArrayList al1=new ArrayList();
13    ArrayList al2=new ArrayList();
14    //ArrayList al3=new ArrayList();
15
16    BufferedReader CSVFile1 = new BufferedReader(new FileReader(path+file1));
17    String dataRow1 = CSVFile1.readLine();
18    while (dataRow1 != null)
19    {
20        String[] dataArray1 = dataRow1.split(",");
21        for (String item1:dataArray1)
22        { 
23           al1.add(item1);
24        }
25
26        dataRow1 = CSVFile1.readLine(); // Read next line of data.
27    }
28
29     CSVFile1.close();
30
31    BufferedReader CSVFile2 = new BufferedReader(new FileReader(path+file2));
32    String dataRow2 = CSVFile2.readLine();
33    while (dataRow2 != null)
34    {
35        String[] dataArray2 = dataRow2.split(",");
36        for (String item2:dataArray2)
37        { 
38           al2.add(item2);
39
40        }
41        dataRow2 = CSVFile2.readLine(); // Read next line of data.
42    }
43     CSVFile2.close();
44
45     for(String bs:al2)
46     {
47         al1.remove(bs);
48     }
49
50     int size=al1.size();
51     System.out.println(size);
52
53     try
54        {
55            FileWriter writer=new FileWriter(path+file3);
56            while(size!=0)
57            {
58                size--;
59                writer.append(""+al1.get(size));
60                writer.append('\n');
61            }
62            writer.flush();
63            writer.close();
64        }
65        catch(IOException e)
66        {
67            e.printStackTrace();
68        }
69}}