Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Kotlin Program to Convert OutputStream to String
In this program, you'll learn to convert outputstream to a string using String initializer in Kotlin.
Example: Convert OutputStream to String
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 you run the program, the output will be:
Hello there!
In the above program, we've created an OutputStream based on the given string line. This is done using stream's write() method.
Then, we simply convert the OutputStream to finalString using String's constructor which takes byte array. For this, we use stream's toByteArray() method.
Here's the equivalent Java code: Java program to convert OutputStream to String.
Did you find this article helpful?