You can simplify your function by using the copyTo function:
fun File.copyInputStreamToFile(inputStream: InputStream) {
this.outputStream().use { fileOut ->
inputStream.copyTo(fileOut)
}
}
Answer from yole on Stack OverflowAndroid Developers
developer.android.com › api reference › fileinputstream
FileInputStream | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Compile N Run
compilenrun.com › kotlin tutorial › kotlin io › kotlin input output streams
Kotlin Input Output Streams | Compile N Run
Here's a basic example of reading ... fun main() { val file = File("sample.txt") // Using FileInputStream (byte stream) FileInputStream(file).use { fis -> val bytes = ByteArray(file.length().toInt()) fis.read(bytes) println("File content (as bytes converted to string): ${String(bytes)}") } // More idiomatic ...
Top answer 1 of 5
107
You can simplify your function by using the copyTo function:
fun File.copyInputStreamToFile(inputStream: InputStream) {
this.outputStream().use { fileOut ->
inputStream.copyTo(fileOut)
}
}
2 of 5
17
My proposition is:
fun InputStream.toFile(path: String) {
File(path).outputStream().use { this.copyTo(it) }
}
without closing current stream
InputStream.toFile("/path/filename")
also, do not forget to handle exceptions, for example if write permission is denied :)
TutorialKart
tutorialkart.com › kotlin › read-contents-of-a-file-in-kotlin
How to Read a File in Kotlin using BufferedReader or InputStream
June 19, 2017 - Kotlin Read File - To read contents of file, use extension methods : Kotlin bufferedReader() - read file to BufferedReader; inputStream() - read file to InputStream; Kotlin forEachLine() - read file line by line; readBytes() - read file to ...
O'Reilly
oreilly.com › library › view › kotlin-programming-by › 9781788474542 › 9746b052-8766-427d-b298-26eb5d9bf64f.xhtml
Reading files from internal storage - Kotlin Programming By Example [Book]
March 28, 2018 - private fun readFile(fileName: String) { val stream: FileInputStream = openFileInput(fileName) val data = ByteArray(1024) stream.read(data) stream.close()}
Author Iyanu Adelekan
Published 2018
Pages 500
Top answer 1 of 8
86
1. Using BufferedReader
import java.io.File
import java.io.BufferedReader
fun main(args: Array<String>) {
val bufferedReader: BufferedReader = File("example.txt").bufferedReader()
val inputString = bufferedReader.use { it.readText() }
println(inputString)
}
2. Using InputStream
Read By Line
import java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream: InputStream = File("example.txt").inputStream()
val lineList = mutableListOf<String>()
inputStream.bufferedReader().forEachLine { lineList.add(it) }
lineList.forEach{println("> " + it)}
}
Read All Lines
import java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream: InputStream = File("example.txt").inputStream()
val inputString = inputStream.bufferedReader().use { it.readText() }
println(inputString)
}
3. Use File directly
import java.io.File
import java.io.BufferedReader
fun main(args: Array<String>) {
val lineList = mutableListOf<String>()
File("example.txt").useLines { lines -> lines.forEach { lineList.add(it) }}
lineList.forEach { println("> " + it) }
}
2 of 8
40
I think the simplest way to code is using kotlin.text and java.io.File
import java.io.File
fun main(args: Array<String>) {
val text = File("sample.txt").readText()
println(text)
}
ZetCode
zetcode.com › kotlin › readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - The example reads a text file into a byte array. It prints the file as numbers to the console. ... In this article we have shown how to read file in Kotlin.
Android Developers
developer.android.com › api reference › inputstream
InputStream | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
YouTube
youtube.com › watch
InputStreams & OutputStreams In Kotlin - IO Essentials - YouTube
In this video you'll learn about input and output streams in Kotlin and how we can use these to read data from and write data to files. ⭐ Courses with real-l...
Published January 22, 2025
Programiz
programiz.com › kotlin-programming › examples › convert-inputstream-string
Kotlin Program to Convert InputStream to String
In this program, you'll learn to convert input stream to a string using InputStreamReader in Kotlin.