CSV File Association by Record Numbers

Question

I want to join or you can say union two CSV files in another output .csv file. E.g. say I have a csv file with fields a,b,c,d,e, and another with fields a,b,c,x,y,z. I need output in the format like this - a,b,c,d,e,x,y,z

I did in the following way but not get desired output. Is there any Java code that works?

int n1 = 3,n2 = 3;//stores the serial number of the column that has the duplicate data

 

BufferedReader br1=new  BufferedReader(new  InputStreamReader(new  FileInputStream("c:/test/report1.csv")));

 

BufferedReader br2=new  BufferedReader(new  InputStreamReader(new  FileInputStream("c:/test/report2.csv")));

 

String line1,line2;

 

while((line1=br1.readLine())!=null && (line2=br2.readLine())!=null){

 

String line=line1+","+line2;

 

String newL="";

 

StringTokenizer st=new  StringTokenizer(line,",");

 

System.out.println("total tokens"+st.countTokens());

 

int i = 3;

 

for(int i=1;i<=st.countTokens();i++){

 

if((i==n1)||(i==n1+n2))

 

continue;

 

else

 

newL=newL+","+st.nextToken();

 

}

 

String l=newL.substring(1);

 

System.out.println("merged"+l);

//write this line to the output file

 

Answer

This is a union of structured file according to sequence numbers of the records. It’s fairly difficult to achieve it in Java since the high-level language lacks corresponding class libraries. We can try a new method. Here I write the script in espProc SPL (Structured Process Language):

 

A

1

=file("d:\\report1.csv").import@ct()

2

=file("d:\\report2.csv").import@ct()

3

=join@p(A1;A2).new(_1.a   ,_1.b ,_1.c ,_1.d ,_1.e ,_2.x ,_2.y ,_2.z)

4

=file("D:\\result.csv").export@t(A3;",")

A1-A2: Import the two CSV files respectively.

A3: Union the two tables by record numbers to get the desired fields.

A4: Write the result set to result.csv.

Most of the time files are joined by certain fields, which is similar to a SQL join. Find more examples in Structured Text File Processing in SPL (II).

An esProc SPL script can integrate with a Java program through esProc JDBC. Details are explained in How to Call an SPL Script in Java.