Shaireen
Shaireen

Reputation: 3703

Join two csv files into one in java

I have two csv files with multiple columns from multiple tables.I am using opencsv to make csv files. I want to make one csv file containing all the columns from both files. There is one common column in both the files. But number of records are not same. Please suggest something. Any help would be appreciated.

P.S.: Joining two files simply mean i want to add all the columns in one file..It is not the database join . I want the combined csv file and use it to in some tool to generate a pdf

Upvotes: 0

Views: 12082

Answers (2)

yurib
yurib

Reputation: 8147

Load one file into a dictionary keyed by the common column value, then append all the records of the 2nd file to the respective entry in the dictionary (again by common column value).
Finally, write all dictionary k,v pairs to a new file.

improvised example:

CSVReader r1 = ...; // reader of 1st file
CSVReader r2 = ...; // reader of 2nd file

HashMap<String,String[]> dic = new HashMap<String,String[]>();

int commonCol = 1; // index of the commonColumn

r1.readNext(); // skip header
String[] line = null;
while ((line = r1.readNext()) != null)
{
  dic.add(line[commonCol],line)
}

commonCol = 2; // index of the commonColumn in the 2nd file

r2.readNext(); // skip header
String[] line = null;
while ((line = r2.readNext()) != null)
{
  if (dic.keySet().contains(line[commonCol])
  {
    // append line to existing entry
  }
  else
  {
     // create a new entry and pre-pend it with default values
     // for the columns of file1
  }
}

foreach (String[] line : dic.valueSet())
{
   // write line to the output file.
}

Upvotes: 4

Sankha Narayan Guria
Sankha Narayan Guria

Reputation: 945

We can do something like this if we know which columns have the duplicate data

    int n1,n2;//stores the serial number of the column that has the duplicate data
    BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(f1)));
    BufferedReader br2=new BufferedReader(new InputStreamReader(new FileInputStream(f2)));
    String line1,line2;
    while((line1=br1.readLine())!=null && (line2=br2.readLine())!=null){
        String line=line1+","+line2;
        String newL="";
        StringTokenizer st=new StringTokenizer(line,",");
        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);
        //write this line to the output file
    }

Upvotes: 0

Related Questions