遍历通过参数指定的行
loop through the lines which are specified through a parameter
下面有一段代码从 s3 中的文件读取并处理所有行。该文件中没有任何行号。但是,我希望代码只处理特定的文件行。我应该如何处理?
注意:起始行号和结束行号作为参数传递。
下面是当前代码
var counter=0
s3Service.getS3FileBufferedReader(s3File).use { s3BufferedReader ->
s3BufferedReader.useLines { lines ->
lines.forEach {
// Process each line
val transaction = Transaction(it)
// does some processing
counter ++
log.info("Line number: ${counter}")
}
}
}```
What I'm trying to achieve is, instead of processing the whole file, I want to process lines from 10 to 20. The catch is, the file only has data and no line numbers in it.
您可以为此使用 Sequence#drop(Int)
和 Sequence#take(Int)
:
lines.drop(start - 1).take(end - start + 1).forEach {
// do your stuff with the line
}
下面有一段代码从 s3 中的文件读取并处理所有行。该文件中没有任何行号。但是,我希望代码只处理特定的文件行。我应该如何处理? 注意:起始行号和结束行号作为参数传递。 下面是当前代码
var counter=0
s3Service.getS3FileBufferedReader(s3File).use { s3BufferedReader ->
s3BufferedReader.useLines { lines ->
lines.forEach {
// Process each line
val transaction = Transaction(it)
// does some processing
counter ++
log.info("Line number: ${counter}")
}
}
}```
What I'm trying to achieve is, instead of processing the whole file, I want to process lines from 10 to 20. The catch is, the file only has data and no line numbers in it.
您可以为此使用 Sequence#drop(Int)
和 Sequence#take(Int)
:
lines.drop(start - 1).take(end - start + 1).forEach {
// do your stuff with the line
}