Search code examples
csvgroovyopencsv

CSV parser skips the empty cell by default


Here is my csv

0|A0|B0||D0
1|A1|B1|C1|D1

Here is the csvParser i am using

 file.eachLine() { line ->
                if (!line.startsWith("0")) {
                    def field = line.tokenize("|")
                    lineCount++
                    closure(lineCount,field)
                }
                else{   
                }
            }

So in the first line of CSV it doesnt have C), How can I say read that empty line too.


Solution

  • this also does the trick:

    file.splitEachLine( /\|/ ){ String[] parts ->
      if( '0' != parts[ 0 ] ){
        //do something with parts
      }else{}
    }
    

    God bless groovy! :)