In Kotlin, how do I read the entire contents of an InputStream into a String?

前端 未结 3 446
小蘑菇
小蘑菇 2020-12-04 16:19

I recently saw code for reading entire contents of an InputStream into a String in Kotlin, such as:

// input is of type InputStream
val baos =         


        
3条回答
  •  萌比男神i
    2020-12-04 16:32

    An example that reads contents of an InputStream to a String

    import java.io.File
    import java.io.InputStream
    import java.nio.charset.Charset
    
    fun main(args: Array) {
        val file = File("input"+File.separator+"contents.txt")
        var ins:InputStream = file.inputStream()
        var content = ins.readBytes().toString(Charset.defaultCharset())
        println(content)
    }
    

    For Reference - Kotlin Read File

提交回复
热议问题