Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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 :: android press back 2 times to exit 
Java :: Printing cube root of a number in java 
Java :: java regular expressions 
Java :: Bukkit plugin player variable 
Java :: jpql spring boot 
Java :: width and height of screen java 
Java :: de caracter a string en java 
Java :: install oracle java ubuntu 20.04 
Java :: java enum strings 
Java :: java hashmap increase value by 1 
Java :: find out duplicate number between 1 to n numbers in java 
Java :: maths.random in Java 
Java :: fix nullpointerexception 
Java :: firebase realtime database get key 
Java :: Java Define a Functional Interface in java 
Java :: LRU Cache java 
Java :: getstring java 
Java :: exception class implementation in java 
Java :: java integer object to char 
Java :: encapsulation java 
Java :: search in rotated sorted array leetcode 
Java :: get string size 
Java :: compare 2 hashmap 
Java :: java servlet life cycle 
Java :: java add values to array 
Java :: configure JWT on Springboot 
Java :: calculating the percentile in java 
Java :: java hashmap set value 
Java :: update a key in the firebase realtime database java 
Java :: edit text on 2sec change andropid 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =