Fork me on GitHub

Partial writing

Partial writing allows you to handle optional values in your data.

The full source for these examples can be found here.

Partial writing with CsvBeanWriter

As you can see in this example, we're only writing 5 of the available fields from the bean and 2 of those are optional.

This example demonstrates the two options you have when writing optional fields:

  1. specifying a default value if the value is null by using new ConvertNullTo() - in this case "no response" is written when married is null.
  2. writing an empty column if the value is null - as is done by specifying new Optional() for numberOfKids (null would have the same effect as new Optional(), but it's not as meaningful)
/**
 * An example of partial reading using CsvBeanWriter.
 */
private static void partialWriteWithCsvBeanWriter() throws Exception {
        
        // create the customer beans
        final CustomerBean john = new CustomerBean("1", "John", "Dunbar",
                new GregorianCalendar(1945, Calendar.JUNE, 13).getTime(),
                "1600 Amphitheatre Parkway\nMountain View, CA 94043\nUnited States", null, null,
                "\"May the Force be with you.\" - Star Wars", "jdunbar@gmail.com", 0L);
        final CustomerBean bob = new CustomerBean("2", "Bob", "Down",
                new GregorianCalendar(1919, Calendar.FEBRUARY, 25).getTime(),
                "1601 Willow Rd.\nMenlo Park, CA 94025\nUnited States", true, 0,
                "\"Frankly, my dear, I don't give a damn.\" - Gone With The Wind", "bobdown@hotmail.com", 123456L);
        final List<CustomerBean> customers = Arrays.asList(john, bob);
        
        ICsvBeanWriter beanWriter = null;
        try {
                beanWriter = new CsvBeanWriter(new FileWriter("target/partialWriteWithCsvBeanWriter.csv"),
                        CsvPreference.STANDARD_PREFERENCE);
                
                // only map 5 of the 10 fields
                final String[] header = new String[] { "customerNo", "firstName", "lastName", "married", "numberOfKids" };
                
                // assign a default value for married (if null), and write numberOfKids as an empty column if null
                final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                        new NotNull(), new ConvertNullTo("no response", new FmtBool("yes", "no")), new Optional() };
                
                // write the header
                beanWriter.writeHeader(header);
                
                // write the customer beans
                for( final CustomerBean customer : customers ) {
                        beanWriter.write(customer, header, processors);
                }
                
        }
        finally {
                if( beanWriter != null ) {
                        beanWriter.close();
                }
        }
}

Output:

customerNo,firstName,lastName,married,numberOfKids
1,John,Dunbar,no response,
2,Bob,Down,yes,0

Partial writing with CsvListWriter

This example is identical to the one above, but uses CsvListWriter.

/**
 * An example of partial reading using CsvListWriter.
 */
private static void partialWriteWithCsvListWriter() throws Exception {
        
        final String[] header = new String[] { "customerNo", "firstName", "lastName", "married", "numberOfKids" };
        
        // create the customer Lists (CsvListWriter also accepts arrays!)
        final List<Object> john = Arrays.asList(new Object[] { "1", "John", "Dunbar",null, null});
        final List<Object> bob = Arrays.asList(new Object[] { "2", "Bob", "Down", true, 0 });
        
        ICsvListWriter listWriter = null;
        try {
                listWriter = new CsvListWriter(new FileWriter("target/partialWriteWithCsvListWriter.csv"),
                        CsvPreference.STANDARD_PREFERENCE);
                
                // assign a default value for married (if null), and write numberOfKids as an empty column if null
                final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                        new NotNull(), new ConvertNullTo("no response", new FmtBool("yes", "no")), new Optional() };
                
                // write the header
                listWriter.writeHeader(header);
                
                // write the customer Lists
                listWriter.write(john, processors);
                listWriter.write(bob, processors);
                
        }
        finally {
                if( listWriter != null ) {
                        listWriter.close();
                }
        }
}

Output:

customerNo,firstName,lastName,married,numberOfKids
1,John,Dunbar,no response,
2,Bob,Down,yes,0

Partial writing with CsvMapWriter

This example is identical to the others above, but uses CsvMapWriter. It also demonstrates that a null cell processor has the same effect as using new Optional().

/**
 * An example of partial reading using CsvMapWriter.
 */
private static void partialWriteWithCsvMapWriter() throws Exception {
        
        final String[] header = new String[] { "customerNo", "firstName", "lastName", "married", "numberOfKids" };
        
        // create the customer Maps (using the header elements for the column keys)
        final Map<String, Object> john = new HashMap<String, Object>();
        john.put(header[0], "1");
        john.put(header[1], "John");
        john.put(header[2], "Dunbar");
        john.put(header[3], null);
        john.put(header[4], null);
        
        final Map<String, Object> bob = new HashMap<String, Object>();
        bob.put(header[0], "2");
        bob.put(header[1], "Bob");
        bob.put(header[2], "Down");
        bob.put(header[3], true);
        bob.put(header[4], 0);
        
        ICsvMapWriter mapWriter = null;
        try {
                mapWriter = new CsvMapWriter(new FileWriter("target/partialWriteWithCsvMapWriter.csv"),
                        CsvPreference.STANDARD_PREFERENCE);
                
                // assign a default value for married (if null), and write numberOfKids as an empty column if null
                final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                        new NotNull(), new ConvertNullTo("no response", new FmtBool("yes", "no")), null };
                
                // write the header
                mapWriter.writeHeader(header);
                
                // write the customer Maps
                mapWriter.write(john, header, processors);
                mapWriter.write(bob, header, processors);
                
        }
        finally {
                if( mapWriter != null ) {
                        mapWriter.close();
                }
        }
}

Output:

customerNo,firstName,lastName,married,numberOfKids
1,John,Dunbar,no response,
2,Bob,Down,yes,0