java 在 Kotlin 中使用 BufferedReader 的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41000584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:36:37  来源:igfitidea点击:

Best way to use BufferedReader in Kotlin

javabufferedreaderkotlin

提问by Akira Kido

So I've just started using Kotlin for Android, and converted my Android Java codes into Kotlin.

所以我刚刚开始在 Android 上使用 Kotlin,并将我的 Android Java 代码转换为 Kotlin。

In one of the conversions, I stumbled upon a BufferedReader, which I would usually write in Java as the following:

在其中一次转换中,我偶然发现了一个 BufferedReader,我通常用 Java 编写如下:

String result = "";
String line = "";
BufferedReader reader = new BufferedReader(someStream);
while ( (line = reader.readLine()) != null ) {
    result += line;
}

But in Kotlin, it seems that Kotlin doesn't allow me to assign values to variables in while conditions.

但是在 Kotlin 中,Kotlin 似乎不允许我在 while 条件下为变量赋值。

Currently, I've written the code as the following:

目前,我编写的代码如下:

val reader = BufferedReader(someStream)
var line : String? = ""
while (line != null) {
    line = reader.readLine()
    result += line
}

which I don't find so elegant and feels prev-gen, despite using Kotlin.

尽管使用了 Kotlin,但我觉得它并不那么优雅并且感觉很像以前的版本。

What would be the best way to use BufferedReader in Kotlin?

在 Kotlin 中使用 BufferedReader 的最佳方法是什么?

回答by miensol

You can use bufferedReaderlike so

你可以bufferedReader像这样使用

val allText = inputStream.bufferedReader().use(BufferedReader::readText)

回答by Jo?o Gon?alves

If you still wanted to read it line by line you could use some extension functions from std lib and do it as follows:

如果您仍然想逐行阅读它,您可以使用 std lib 中的一些扩展函数并按如下方式进行:

val reader = someStream.bufferedReader()
val iterator = reader.linesSequences().iterator()
while(iterator.hasNext()) {
    val line = iterator.next()
    // do something with line...
}
reader.close()

or alternatively, using a "functional" approach:

或者,使用“功能”方法:

val reader = someStream.bufferedReader()
reader.useLines {
    it.map { line -> // do something with line }
}

by using useLines, you don't need to explicitly call close on the reader, the useLines extensions function will do it for you!

通过使用 useLines,您不需要在阅读器上显式调用 close,useLines 扩展函数将为您完成!

Just adding those for reference.. cheers

只是添加那些以供参考..干杯

回答by Angel Koh

you can also try using the "forEachLine" method.

您也可以尝试使用“forEachLine”方法。

val file = File("./folder/test.txt")
file.bufferedReader().forEachLine {
    println("value = $it")
} 

it'll also automatically close the stream after reading the last line

它还会在阅读最后一行后自动关闭流

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-reader/index.html

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-reader/index.html

fun Reader.forEachLine(action: (String) -> Unit)
Iterates through each line of this reader, calls action for each line read and closes the Reader when it's completed.

fun Reader.forEachLine(action: (String) -> Unit)
遍历此读取器的每一行,为读取的每一行调用操作并在完成时关闭读取器。

回答by Chris Ritchie

Another way is to use a for loop:

另一种方法是使用 for 循环:

val reader = BufferedReader(someStream)
for (line in reader.lines()) {
    println(line)
}

While is it not as concise as the accepted answer, it will allow you to loop thru and perform some kind of logic without pumping everything into one string as shown below

虽然它不像接受的答案那么简洁,但它允许您循环并执行某种逻辑,而无需将所有内容都放入一个字符串中,如下所示

val allText: String = inputStream.bufferedReader().use(BufferedReader::readText)

回答by Alejandro Moya

Thanks to Jo?o Gon?alves reference to the stdlib I found that you can use forEachLineto traverse the reader if needed.

感谢 Jo?o Gon?alves 对 stdlib 的引用,我发现如果需要,您可以使用forEachLine来遍历阅读器。

回答by reza rahmad

use the code like this

使用这样的代码

    val input = conn.inputStream
    val allText = input.bufferedReader().use(BufferedReader::readText)
    val result = StringBuilder()                   

    result.append(allText)
    return result.toString()

    } else {

    return "unsuccessful"

    }