Kotlin copy file


#1

hi,

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)
}

    inStream.close()
    outStream.close()

tested with “do / while” same thing …

thanks in advance


#2

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


#3

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 …


#4

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)
}

#5

hi,

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)

workonfile.writeText(content)

same thing, my destination file stays empty


#6

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


#7

Are you tried the Kotlin Standard Library?

http://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/index.html

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

#8

hi,
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 …


#9

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


#10

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


#11

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 !