I have a class which is paring csv based file, but I would like to put a parameter for the token symbol.
Please let me know how can I change the function and use the function on program.
class CSVParser{
static def parseCSV(file,closure) {
def lineCount = 0
file.eachLine() { line ->
def field = line.tokenize(';')
lineCount++
closure(lineCount,field)
}
}
}
use(CSVParser.class) {
File file = new File("test.csv")
file.parseCSV { index,field ->
println "row: ${index} | ${field[0]} ${field[1]} ${field[2]}"
}
}
You’ll have to add the parameter in between the
fileandclosureparameters.When you create a category class with static methods, the first parameter is the object the method is being called on so
filemust be first.Having a closure as the last parameter allows the syntax where the open brace of the closure follows the function invocation without parentheses.
Here’s how it would look: