开发者

How do I keep a line number count when using reader.eachLine to read a BufferedInputStream?

开发者 https://www.devze.com 2023-01-05 12:34 出处:网络
How can I keep track of the line number I\'m on when using eachLine to read a BufferedInputStream? def input = new GZIPInputStream(new FileInputStream(f))

How can I keep track of the line number I'm on when using eachLine to read a BufferedInputStream?

def input = new GZIPInputStream(new FileInputStream(f))
def reader = new BufferedReader(new InputStreamReader(input))
reader.eachLine {
    line ->if(line.con开发者_StackOverflow中文版tains(searchString)){
        println "${f} - ${line}"
    }
}


The closure you pass to eachLine can also take 2 parameters. First being the line of data and the second being the line number.

....
....
reader.eachLine { line, lineNumber ->
    if(line.contains(searchString)) {
        println "${lineNumber} - ${line}"
    }
}

See GDK Doc for InputStream eachLine method.

0

精彩评论

暂无评论...
验证码 换一张
取 消