如何在eachLine中使用firstLine参数 [英] How do I use the firstLine argument in eachLine

查看:118
本文介绍了如何在eachLine中使用firstLine参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法让eachLine跳过第一行,据此,有一个整数参数可以传递给eachLine但我不知道语法

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
}

推荐答案

我认为您误解了"firstLine"参数的用途.从文档中:

I think you are misunderstanding what the 'firstLine' parameter is used for. From the docs:

firstLine-用于第一行的行号值

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.

对于以下代码:

new FileReader('c:/users/chris/desktop/file.txt').eachLine(4){line, number-> 
    println "$number $line"
}

它将打印出来:

4行1

5行2

6行3

这篇关于如何在eachLine中使用firstLine参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆