44

I have a large stream of text coming back from REST web service and I would like to write it directly to file. What is the simplest way of doing this?

I have written the following function extension that WORKS. But I can't help thinking that there is a cleaner way of doing this.

Note: I was hoping to use try with resources to auto close the stream and file

fun File.copyInputStreamToFile(inputStream: InputStream) {
    val buffer = ByteArray(1024)

    inputStream.use { input ->
        this.outputStream().use { fileOut ->

            while (true) {
                val length = input.read(buffer)
                if (length <= 0)
                    break
                fileOut.write(buffer, 0, length)
            }
            fileOut.flush()
        }
    }
}

5 Answers 5

101

You can simplify your function by using the copyTo function:

fun File.copyInputStreamToFile(inputStream: InputStream) {
    this.outputStream().use { fileOut ->
        inputStream.copyTo(fileOut)
    }
}
3
  • 6
    The outer use seems like a mistake. You are closing/disposing in a different scope than the one in which inputStream was opened. Sep 26, 2017 at 7:08
  • 2
    And to do the opposite (write from file to some outputStream), I use this: FileInputStream(someFile).use { stream -> stream.copyTo(someOutputStream) } , right? Apr 28, 2019 at 12:54
  • 1
    Andre's comment is no longer relevant as the answer has been edited
    – Hack5
    Mar 26, 2022 at 14:51
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 :)

10

I suggest to make like this:

fun InputStream.toFile(path: String) {
    use { input ->
        File(path).outputStream().use { input.copyTo(it) }
    }
}

and then to use like:

InputStream.toFile("/some_path_to_file")
1
  • You should always close in the same scope you opened in. This is likely the reason copyTo doesn't automatically close the resources for you. Sep 26, 2017 at 7:13
0

You needs to do like this

@Throws
fun copyDataBase() {

        var myInput = context.getAssets().open(DB_NAME)
        var outFileName = DB_PATH + DB_NAME
        var fileOut: OutputStream = FileOutputStream(outFileName)
        val buffer: ByteArray = ByteArray(1024)
        var length: Int? = 0

        while (true) {
            length = myInput.read(buffer)
            if (length <= 0)
                break
            fileOut.write(buffer, 0, length)
        }

        fileOut.flush()
        fileOut.close()
        myInput.close()

        throw IOException()
}
2
-1

What appears to have worked for me is this:

fun fileCopyer(localFileA: File, localFileB: File) {
var output = localFileA.inputStream()
output.copyTo(localFileB.outputStream())
output.close()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.