CSV parser with groovy categories

This shows how categories in groovy can be used to create a simple csv parser.

To parse a csv file with the following content:
A1,B1,C1
A2,B2,C2
A3,B3,C3
A4,B4,C4
A5,B5,C5

The following example can be used. The CSVParser has all the logic.

class CSVParser {
	static def parseCSV(file,closure) {
		def lineCount = 0
		file.eachLine() { line ->
			def field = line.tokenize(",")
			lineCount++
			closure(lineCount,field)
		}
	}
}

To call it you simply say
parseCSV

:

use(CSVParser.class) {
	File file = new File("test.csv")
	file.parseCSV { index,field ->
		println "row: ${index} | ${field[0]} ${field[1]} ${field[2]}"
	}
}
row: 1 | A1 B1 C1
row: 2 | A2 B2 C2
row: 3 | A3 B3 C3
row: 4 | A4 B4 C4
row: 5 | A5 B5 C5