Question

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.

Was it helpful?

Solution 2

this also does the trick:

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

God bless groovy! :)

OTHER TIPS

tokenize throws away empty tokens and returns a List<String>, try using split instead which returns a String[] and doesn't throw them away (see here for other differences)

Or use something like GroovyCSV and move the pain onto someone else's shoulders ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top