遍历通过参数指定的行

问题描述

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...