使用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.
更多推荐
发布评论