I have a string:
var myString:String = "My String"
How can I convert it to an InputStream
in Kotlin?
Creates an input stream for reading data from this byte array. Creates an input stream for reading data from the specified portion of this byte array.
To convert a byte array to string in Kotlin, use String() constructor. String() constructor can take a Byte Array as argument and return a new string formed with the bytes in the given array.
Kotlin has an extension for String
to convert directly.
val inputStream: InputStream = myString.byteInputStream()
The argument on byteInputStream
is defaulted to charset: Charset = Charsets.UTF_8
.
You can look at the extension by writing it and then cmd+click on it or in the package kotlin.io
file IOStream.kt
Relying on the Java version is not wrong, but rather using a more kotlin idiomatic way when possible
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With