Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the firstLine argument in eachLine

Tags:

groovy

I can't seem to make eachLine skip the first line, according to this there is an integer argument that can be passed to eachLine but I can't figure out the syntax

http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html#eachLine(int, groovy.lang.Closure)

#doesn't work
new FileReader('myfile.txt').eachLine firstLine=2,{
       line-> println line
}
#nope
new FileReader('myfile.txt').eachLine(2){
       line-> println line
}
like image 242
Jeremy Leipzig Avatar asked Apr 23 '10 15:04

Jeremy Leipzig


1 Answers

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

like image 75
Chris Dail Avatar answered Oct 25 '22 03:10

Chris Dail