Kotlin Program to Convert OutputStream to String

Kotlin Program to Convert OutputStream to String. This is done using the stream’s write() method. Then, we simply convert the OutputStream to a final string using String ‘s constructor which takes a byte array.

Kotlin Program to Convert OutputStream to String

Source Code

import java.io.*

fun main(args: Array<String>) {

    val stream = ByteArrayOutputStream()
    val line = "Hello are you there!"

    stream.write(line.toByteArray())
    val finalString = String(stream.toByteArray())

    println(finalString)

}

Output