Fork me on GitHub

Partial reading

Partial reading allows you to ignore columns when reading CSV files by simply setting the appropriate header columns to null.

The examples on this page use the same example CSV file as the reading examples, and the full source can be found here.

Partial reading with CsvBeanReader

As you can see from the output of this example, the fields associated with the ignored columns kept their default values - only the customerNo, firstName, and lastName are populated.

Also note that the cell processors associated with the ignored columns were also set to null to avoid any unnecessary processing (cell processors are always executed).

/**
 * An example of partial reading using CsvBeanReader.
 */
private static void partialReadWithCsvBeanReader() throws Exception {
        
        ICsvBeanReader beanReader = null;
        try {
                beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
                
                beanReader.getHeader(true); // skip past the header (we're defining our own)
                
                // only map the first 3 columns - setting header elements to null means those columns are ignored
                final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
                        null, null };
                
                // no processing required for ignored columns
                final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                        new NotNull(), null, null, null, null, null, null, null };
                
                CustomerBean customer;
                while( (customer = beanReader.read(CustomerBean.class, header, processors)) != null ) {
                        System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(),
                                beanReader.getRowNumber(), customer));
                }
                
        }
        finally {
                if( beanReader != null ) {
                        beanReader.close();
                }
        }
}

Output:

lineNo=4, rowNo=2, customer=CustomerBean [customerNo=1, firstName=John, lastName=Dunbar, birthDate=null, mailingAddress=null, married=null, numberOfKids=null, favouriteQuote=null, email=null, loyaltyPoints=0]
lineNo=7, rowNo=3, customer=CustomerBean [customerNo=2, firstName=Bob, lastName=Down, birthDate=null, mailingAddress=null, married=null, numberOfKids=null, favouriteQuote=null, email=null, loyaltyPoints=0]
lineNo=10, rowNo=4, customer=CustomerBean [customerNo=3, firstName=Alice, lastName=Wunderland, birthDate=null, mailingAddress=null, married=null, numberOfKids=null, favouriteQuote=null, email=null, loyaltyPoints=0]
lineNo=13, rowNo=5, customer=CustomerBean [customerNo=4, firstName=Bill, lastName=Jobs, birthDate=null, mailingAddress=null, married=null, numberOfKids=null, favouriteQuote=null, email=null, loyaltyPoints=0]

Partial reading with CsvMapReader

As you can see from the output of this example, the output Map only has entries for customerNo, firstName, and lastName - the other fields were ignored.

Unlike the CsvBeanReader example above, this example defines processors for all columns. This means that constraint validation is still applied to the ignored columns, but they don't appear in the output Map.

/**
 * An example of partial reading using CsvMapReader.
 */
private static void partialReadWithCsvMapReader() throws Exception {
        
        ICsvMapReader mapReader = null;
        try {
                mapReader = new CsvMapReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
                
                mapReader.getHeader(true); // skip past the header (we're defining our own)
                
                // only map the first 3 columns - setting header elements to null means those columns are ignored
                final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
                        null, null };
                
                // apply some constraints to ignored columns (just because we can)
                final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
                        new NotNull(), new NotNull(), new NotNull(), new Optional(), new Optional(), new NotNull(),
                        new NotNull(), new LMinMax(0L, LMinMax.MAX_LONG) };
                
                Map<String, Object> customerMap;
                while( (customerMap = mapReader.read(header, processors)) != null ) {
                        System.out.println(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader.getLineNumber(),
                                mapReader.getRowNumber(), customerMap));
                }
                
        }
        finally {
                if( mapReader != null ) {
                        mapReader.close();
                }
        }
}

Output:

lineNo=4, rowNo=2, customerMap={lastName=Dunbar, customerNo=1, firstName=John}
lineNo=7, rowNo=3, customerMap={lastName=Down, customerNo=2, firstName=Bob}
lineNo=10, rowNo=4, customerMap={lastName=Wunderland, customerNo=3, firstName=Alice}
lineNo=13, rowNo=5, customerMap={lastName=Jobs, customerNo=4, firstName=Bill}