0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RSM
  • 423
  • 3
  • 7
  • 20

2 Answers2

2

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 ;-)

tim_yates
  • 167,322
  • 27
  • 342
  • 338
1

this also does the trick:

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

God bless groovy! :)

injecteer
  • 20,038
  • 4
  • 45
  • 89