문제

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.

도움이 되었습니까?

해결책 2

this also does the trick:

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

God bless groovy! :)

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top