English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of Kotlin Examples
In this program, you will learn how to convert an OutputStream to a string using string initializer in Kotlin.
import java.io.* fun main(args: Array<String>) { val stream = ByteArrayOutputStream() val line = "Hello there!" stream.write(line.toByteArray()) val finalString = String(stream.toByteArray()) println(finalString) }
When running the program, the output is:
Hello there!
In the above program, we created an OutputStream based on the given string line. This is done using the stream's write() method.
Then, we just use the String constructor to convert OutputStream to finalString, which accepts a byte array. For this, we use the stream's toByteArray() method.
This is the equivalent Java code:Java Program to Convert OutputStream to String.