使用reader.eachLine读取BufferedInputStream时,如何保持行号计数?(How do I keep a line number count when using reade

系统教程 行业动态 更新时间:2024-06-14 16:57:40
使用reader.eachLine读取BufferedInputStream时,如何保持行号计数?(How do I keep a line number count when using reader.eachLine to read a BufferedInputStream?)

在使用eachLine读取BufferedInputStream时,如何跟踪我所在的行号?

def input = new GZIPInputStream(new FileInputStream(f)) def reader = new BufferedReader(new InputStreamReader(input)) reader.eachLine { line ->if(line.contains(searchString)){ println "${f} - ${line}" } }

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.contains(searchString)){ println "${f} - ${line}" } }

最满意答案

您传递给每个线的封闭也可以带2个参数。 首先是数据行,第二行是行号。

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

请参阅GDK Doc for InputStream eachLine方法 。

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.

更多推荐

本文发布于:2023-04-13 12:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/81b023e767760a9f277dd76ecf6cc141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行号   BufferedInputStream   eachLine   reader   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!