问题描述
我在下面有一段代码,它从 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
}