Search code examples
javacsvfile-iosupercsv

Writing back to a csv with Java using super csv


Ive been working on this code for quite sometime and just want to be given the simple heads up if im routing down a dead end. The point where im at now is to mathch identical cells from diffrent .csv files and copy one row into another csv file. The question really is would it be possible to write at specfic lines say for example if the the 2 cells match at row 50 i wish to write back on to row 50. Im assuming that i would maybe extract everything to a hashmap, write it in there then write back to the .csv file? is there a easier way?

for example i have one Csv that has person details, and the other has property details of where the actual person lives, i wish to copy the property details to the person csv, aswell as match them up with the correct person detail. hope this makes sense

public class Old {
 public static void main(String [] args) throws IOException
 {
   List<String[]> cols;
   List<String[]> cols1;

   int row =0;
   int count= 0;
   boolean b;
   CsvMapReader Reader = new CsvMapReader(new FileReader("file1.csv"), CsvPreference.EXCEL_PREFERENCE);
   CsvMapReader Reader2 = new CsvMapReader(new FileReader("file2.csv"), CsvPreference.EXCEL_PREFERENCE);

   try {
       cols = readFile("file1.csv");
       cols1 = readFile("fiel2.csv");
       String [] headers = Reader.getCSVHeader(true);

       headers = header(cols1,headers          

           } catch (IOException e) {
       e.printStackTrace();
       return;
   }

   for (int j =1; j<cols.size();j++) //1
   {
       for (int i=1;i<cols1.size();i++){
           if (cols.get(j)[0].equals(cols1.get(i)[0]))
           {


           }
       }

   }

}


private static List<String[]> readFile(String fileName) throws IOException
{
   List<String[]> values = new ArrayList<String[]>();
   Scanner s = new Scanner(new File(fileName));
   while (s.hasNextLine()) {
       String line = s.nextLine();
       values.add(line.split(","));
   }
   return values;
}
public static void csvWriter (String fileName, String [] nameMapping ) throws FileNotFoundException
{
    ICsvListWriter writer = new CsvListWriter(new PrintWriter(fileName),CsvPreference.STANDARD_PREFERENCE);
    try {
        writer.writeHeader(nameMapping);

    } catch (IOException e) {

        e.printStackTrace();
    }

}
public static String[] header(List<String[]> cols1, String[] headers){
    List<String> list = new ArrayList<String>();
    String [] add;
    int count= 0;
    for (int i=0;i<headers.length;i++){
        list.add(headers[i]);
    }

    boolean c;
    c= true;
    while(c)        {           
        add = cols1.get(0);
        list.add(add[count]);
        if (cols1.get(0)[count].equals(null))// this line is never read errpr
        {               
            c=false;
            break;
        } else  
        count ++;

    }

    String[] array = new String[list.size()];
    list.toArray(array);
    return array;

}

Solution

  • Just be careful if you read all of the addresses and person details into memory first (as Thomas has suggested) - if you're only dealing with small CSV files then it's fine, but you may run out of memory if you're dealing with larger files.

    As an alternative, I've put together an example that reads the addresses in first, then writes the combined person/address details while it reads in the person details.

    Just a few things to note:

    • I've used CsvMapReader and CsvMapWriter because you were - this meant I've had to use a Map containing a Map for storing the addresses. Using CsvBeanReader/CsvBeanWriter would make this a bit more elegant.

    • The code from your question doesn't actually use Super CSV to read the CSV (you're using Scanner and String.split()). You'll run into issues if your CSV contains commas in the data (which is quite possible with addresses), so it's a lot safer to use Super CSV, which will handle escaped commas for you.

    Example:

    package example;
    
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.supercsv.io.CsvMapReader;
    import org.supercsv.io.CsvMapWriter;
    import org.supercsv.io.ICsvMapReader;
    import org.supercsv.io.ICsvMapWriter;
    import org.supercsv.prefs.CsvPreference;
    
    public class CombiningPersonAndAddress {
    
        private static final String PERSON_CSV = "id,firstName,lastName\n"
                + "1,philip,fry\n2,amy,wong\n3,hubert,farnsworth";
    
        private static final String ADDRESS_CSV = "personId,address,country\n"
                + "1,address 1,USA\n2,address 2,UK\n3,address 3,AUS";
    
        private static final String[] COMBINED_HEADER = new String[] { "id",
                "firstName", "lastName", "address", "country" };
    
        public static void main(String[] args) throws Exception {
    
            ICsvMapReader personReader = null;
            ICsvMapReader addressReader = null;
            ICsvMapWriter combinedWriter = null;
            final StringWriter output = new StringWriter();
    
            try {
                // set up the readers/writer
                personReader = new CsvMapReader(new StringReader(PERSON_CSV),
                        CsvPreference.STANDARD_PREFERENCE);
                addressReader = new CsvMapReader(new StringReader(ADDRESS_CSV),
                        CsvPreference.STANDARD_PREFERENCE);
                combinedWriter = new CsvMapWriter(output,
                        CsvPreference.STANDARD_PREFERENCE);
    
                // map of personId -> address (inner map is address details)
                final Map<String, Map<String, String>> addresses = 
                        new HashMap<String, Map<String, String>>();
    
                // read in all of the addresses
                Map<String, String> address;
                final String[] addressHeader = addressReader.getCSVHeader(true);
                while ((address = addressReader.read(addressHeader)) != null) {
                    final String personId = address.get("personId");
                    addresses.put(personId, address);
                }
    
                // write the header
                combinedWriter.writeHeader(COMBINED_HEADER);
    
                // read each person
                Map<String, String> person;
                final String[] personHeader = personReader.getCSVHeader(true);
                while ((person = personReader.read(personHeader)) != null) {
    
                    // copy address details to person if they exist
                    final String personId = person.get("id");
                    final Map<String, String> personAddress = addresses.get(personId);
                    if (personAddress != null) {
                        person.putAll(personAddress);
                    }
    
                    // write the combined details
                    combinedWriter.write(person, COMBINED_HEADER);
                }
    
            } finally {
                personReader.close();
                addressReader.close();
                combinedWriter.close();
            }
    
            // print the output
            System.out.println(output);
    
        }
    }
    

    Output:

    id,firstName,lastName,address,country
    1,philip,fry,address 1,USA
    2,amy,wong,address 2,UK
    3,hubert,farnsworth,address 3,AUS