Write InputStream To File

April 13, 2018
val outputStream = FileOutputStream(outputFile)
// the following should work as well 
// val outputStream = outputFile.outputStream()

inputStream.use { input ->
    outputStream.use { output ->
        input.copyTo(output)
    }
}

Convert large string into InputStream.

val input = "I am String"
val inputStream = ByteArrayInputStream(input.toByteArray(UTF_8))

If the InputStream is a file, you can use File.copyTo instead.

inputFile.copyTo(outputFile, true)
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.