CSV files

This week I have been focusing on cleaning up my code and changing my upload and download to use Super CSV.

Super CSV is a very good tool for reading and writing CSV files. CSV files are simply files which contain multiple rows and each field is delimited by a comma. Some of the difficult parts about working with CSV files is:

  • they may or may not contain headers
  • each field may or may not be enclosed in double quotes
  • within the header and each record, there may be one or more fields, separated by comma
  • the last record in the file may or may not have an ending line break

Super CSV is a library that has been created to help work with some of these difficulties in a csv file.

It has 4 different readers to work with: CsvBeanReader,    CsvDozerBeanReader,    CsvListReader,    CsvMapReader

For my needs I chose the CsvMapReader. It is very useful. First you have to instantiate a reader and then set up your Cell Processors. The cell processors are what help Super CSV effectively parse your file. The cells are then mapped to the header names and put in a Map<String, Object>. Here is an example of the cell processors from the Super CSV site:

private static CellProcessor[] getProcessors() {

        final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust!
        StrRegEx.registerMessage(emailRegex, "must be a valid email address");

        final CellProcessor[] processors = new CellProcessor[] { 
                new UniqueHashCode(), // customerNo (must be unique)
                new NotNull(), // firstName
                new NotNull(), // lastName
                new ParseDate("dd/MM/yyyy"), // birthDate
                new NotNull(), // mailingAddress
                new Optional(new ParseBool()), // married
                new Optional(new ParseInt()), // numberOfKids
                new NotNull(), // favouriteQuote
                new StrRegEx(emailRegex), // email
                new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
        };

        return processors;
}

Here is how you get the cell processor: final CellProcessor[] processors = getProcessors();

and the headers: final String[] header = beanReader.getHeader(true);

Then when you need to know the value of a field you keep up with the row by:

Map<String, Object> exampleMap; 
while( (examleMap = mapReader.read(header, processors)) != null ) {
}

and simply call mapList.get(“object name  which matches header”). It really saves time because there are many ways to parse a csv file but this has taken the best ways and but them into a useful library.

The rest of the week has been spent trying to make fields easy to understand and making it look better.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s