How do I use the firstLine argument in eachLine

I think you are misunderstanding what the 'firstLine' parameter is used for. From the docs:

firstLine - the line number value used for the first line

Basically this means that this number will identify what the first line is. It always goes through each line in the file.

So for the following code:

new FileReader('c:/users/chris/desktop/file.txt').eachLine(4){line, number-> 
    println "$number $line"
}

It would print out:

4 line1

5 line2

6 line3


To skip first line use return. It's works like continue in standard loops.

new FileReader('myfile.txt').eachLine { line, number ->
    if (number == 1)
        return // continue

    println "$number: $line"
}

Tags:

Groovy