That's not directly supported by the closure, but it's easy enough to achieve the same logic if you change things around slightly:
// test script:
def f = new File("test.txt")
def currentLine
f.eachLine { nextLine ->
if (currentLine) {
if (currentLine.find(/hooray/)) {
println "current line: ${currentLine}"
println "next line: ${nextLine}"
}
}
currentLine = nextLine
}
// test.txt contents:
first line
second line
third line
fourth line
fifth hooray line
sixth line
seventh line
Edit:
If you're looking for the encapsulation that Chili commented on below, you could always define your own method on File:
File.metaClass.eachLineWithNextLinePeek = { closure ->
def currentLine
delegate.eachLine { nextLine ->
if (currentLine) {
closure(currentLine, nextLine)
}
currentLine = nextLine
}
}
def f = new File("test.txt")
f.eachLineWithNextLinePeek { currentLine, nextLine ->
if (currentLine.find(/hooray/)) {
println "current line: ${currentLine}"
println "next line: ${nextLine}"
}
}