Kotlin copy file


i want to copy a file.txt to another

my code makes my app crash , during the “while” loop …

for testing purpose, i took 2 files from external storage (permissions are ok )

    var inStream: InputStream? = null
    var outStream: OutputStream? = null
    val afile = File(Environment.getExternalStorageDirectory().toString() + "/Rivescript/Bots/truite.rive")
    val bfile = File(Environment.getExternalStorageDirectory().toString() + "/Rivescript/Bots/Bfile.txt")

    inStream = FileInputStream(afile)
    outStream = FileOutputStream(bfile)

    val buffer = ByteArray(1024)
    val length: Int

   length = inStream.read(buffer)

   while (length    > 0 )

outStream.write(buffer, 0, length)


tested with “do / while” same thing …

thanks in advance

This is an infinite loop. You need to update length.

something this way ?

while ((length = inStream.read(buffer)) > 0 ) {

        outStream.write(buffer, 0, length)


i obtain message from Android studion : assignment are not expressions and only expressions are allowed in this context …

You can’t assing a variable during expression in kotlin (take a look at: Assignment not allow in while expression?)

Try that:

var length = inStream.read(buffer)

while (length > 0) {
	outStream.write(buffer, 0, length)
	length = inStream.read(buffer)


i’ve tried your solution, they is no error anymore but my file stays empty …

i have tried working with string too like that =>

val srcfilename =Environment.getExternalStorageDirectory().toString() + “/Rivescript/Bots/truite.rive”
val content = File(srcfilename).readText()

val destfileName = Environment.getExternalStorageDirectory().toString() + “/Rivescript/Bots/Bfile.txt”

val workonfile = File(destfileName)


same thing, my destination file stays empty

I think you have to use Environment.getExternalStorageDirectory().getAbsolutePath().

Are you tried the Kotlin Standard Library?

File(Environment.getExternalStorageDirectory(), "Rivescript/Bots/truite.rive").copyTo(
    File(Environment.getExternalStorageDirectory(), "Rivescript/Bots/Bfile.txt") )

i 've just tried,

File(Environment.getExternalStorageDirectory(), “Rivescript/Bots/truite.rive”).copyTo(
File(Environment.getExternalStorageDirectory(), “Rivescript/Bots/Bfile.txt”) )
kotlin.io.FileAlreadyExistsException: /storage/208C-ED35/Rivescript/Bots/truite.rive → /storage/208C-ED35/Rivescript/Bots/Bfile.txt: The destination file already exists.

ok so i add the overwrite parameter

File(Environment.getExternalStorageDirectory(), “Rivescript/Bots/truite.rive”).copyTo(
File(Environment.getExternalStorageDirectory(), “Rivescript/Bots/Bfile.txt”) , true)

no exception error but the file is empty

paths are ok in the exception log ,

if i delete the destination file, with overwrite parameter or not, nothing appens , the app doesn’t crash but i have’nt my file …

This is a bug?
The source file is empty?
The destination is writable?

this is a bug ? i don’t know, i’m new in Kotlin

source file empty ? no it’s 58ko
destination is writable ? in theory yes , i can copy / delete files from sdcard but
when opening the .txt from computer with usb , i can’t add characters and save …it’s a hint

sd card files can be seen with the phone but the don’t appear with windows explorer,
my destination file is well updated by copy , i simply look from windows,

i will format my sdcard and will do new tests, this sd was into another phone…i installed it without format …

in fact i think that everything was good from beginning …but many thanks to all that have helped me, i have learned a lot upon file copy !

So here is the tested code and works fine.

 val _in = FileInputStream(File(params[0]))
            val out = FileOutputStream(File(params[1]))

            // Copy the bits from instream to outstream
            val buf = ByteArray(1024)
            var len: Int
             len = _in.read(buf)
            while (len > 0) {

                if (len != -1) {

                    out.write(buf, 0, len)
                    len = _in.read(buf)


            Log.v("", "Copy file successful.")

        } else {
            Log.v("", "Copy file failed. Source file missing.")