Wednesday 12 December 2018

Reading csv files and comparing it

package selenium;

import java.util.ArrayList;
import java.util.HashMap;

public class CSVComparisonMain {

private static ArrayList<String> keys_Source=null;
private static ArrayList<String> keys_Destination = null;
private static HashMap<String, ArrayList> header_Values_Source;
private static HashMap<String, ArrayList> header_Values_Destination;

public static void main(String[] args) throws Exception
{
CSVFIleReader source_Obj = new CSVFIleReader("C:\\Avinash\\source.csv", "EVENT_CD");
source_Obj.readCSVFIle();
keys_Source = source_Obj.keys;
header_Values_Source = source_Obj.header_Values;


CSVFIleReader destination_Obj = new CSVFIleReader("C:\\Avinash\\Destination.csv", "EVENT_CD");
destination_Obj.readCSVFIle();
keys_Destination = destination_Obj.keys;
header_Values_Destination = destination_Obj.header_Values;

System.out.println("Comparison Starts.............!!!");


for (String key : keys_Source) {
if(header_Values_Destination.containsKey(key))
{
ArrayList source_Values =  header_Values_Source.get(key);
ArrayList destination_Values =  header_Values_Destination.get(key);
for (Object value : source_Values)
{
if(!destination_Values.contains(value))
{
throw new Exception("Source file Value: "+value+" is not matched in target file value: "+value);
}
}

}
else
{
throw new Exception ("Exception occured while comparing .csv file.......Key: "+ key +" not found in target file");
}
}
}

}


package selenium;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class CSVFIleReader
{

BufferedReader csvReader = null;
String filePath = null;
String key = null;
public String delimiter = null;
int keyIndex = -1;

HashMap<String, ArrayList> header_Values = null;
ArrayList<String> keys =null;
ArrayList<String> values = null;

public CSVFIleReader(String filePath , String key)
{
this.filePath = filePath;
this.key = key;
header_Values = new HashMap<>();
keys = new ArrayList<>();
values = new ArrayList<>();
}


public void readCSVFIle()
{
String line = null;
int header = 0;
try
{
csvReader = new BufferedReader(new FileReader(filePath));
while((line=csvReader.readLine()) != null)
{
String delimit = getDelimiter(line);
System.out.println(line);
if(delimit != null)
{
++header;
generateKeyValuePair(line,header);
}
}
printCSVFile();
csvReader.close();
csvReader = null;
}
catch(Exception e)
{

}
}


public void generateKeyValuePair(String line, int header)
{
ArrayList<String> values = new ArrayList<>();
try
{
if(delimiter.equalsIgnoreCase("pipe") || delimiter.equalsIgnoreCase("comma"))
{
String[] words = line.split("\\|");
if(words.length > 1)
{
for (String value : words)
{
values.add(value);
}
}
if(words.length <= 1)
{
words = line.split("\\,");
if(words.length > 1 )
{
for (String value : words)
{
values.add(value);
}
}
else
throw new Exception("Exception occured while parsing .csv file. Delilmiter is not valid");
}
else
{
throw new Exception("Exception occured while parsing .csv file. Delilmiter is not valid");
}
if(header == 1) //It Means first line is header
{
if(values.contains(key))
{
keyIndex = values.indexOf(key);
}
}

keys.add(values.get(keyIndex));
header_Values.put(values.get(keyIndex).toString(), values);


}
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}

public String getDelimiter(String line)
{
String words[]  = line.split("//|");
if(words.length>0)
{
delimiter = "pipe";
}
else
{
words = line.split("//,");
if(words.length > 0)
{
delimiter = "comma";
}
}
return delimiter;
}

public void printCSVFile()
{
Set<Entry<String, ArrayList>> entrySet = header_Values.entrySet();
for (Entry<String, ArrayList> entry : entrySet)
{
System.out.print(entry.getKey() );
ArrayList values = entry.getValue();
for (Object value : values) {
System.out.print("         "+value.toString());

}
System.out.println();
}
}


public static void main(String[] args)
{
CSVFIleReader obj = new CSVFIleReader("C:\\Avinash\\Temp.csv", "EVENT_CD");
obj.readCSVFIle();

}

}







No comments:

Post a Comment