jmg12
March 13, 2018, 4:27pm
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
Eliote
March 13, 2018, 4:36pm
2
This is an infinite loop. You need to update length
.
jmg12
March 13, 2018, 4:43pm
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 …
Eliote
March 13, 2018, 4:51pm
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)
}
jmg12
March 14, 2018, 9:51am
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
Eliote
March 14, 2018, 1:13pm
6
I think you have to use Environment.getExternalStorageDirectory().getAbsolutePath()
.
android, sd-card
fvasco
March 14, 2018, 1:38pm
7
Are you tried the Kotlin Standard Library?
File(Environment.getExternalStorageDirectory(), "Rivescript/Bots/truite.rive").copyTo(
File(Environment.getExternalStorageDirectory(), "Rivescript/Bots/Bfile.txt") )
jmg12
March 14, 2018, 2:19pm
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 …
fvasco
March 14, 2018, 2:24pm
9
This is a bug?
The source file is empty?
The destination is writable?
jmg12
March 14, 2018, 3:13pm
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
jmg12
March 15, 2018, 2:28pm
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 !
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)
}
}
_in.close()
out.close()
Log.v("", "Copy file successful.")
} else {
Log.v("", "Copy file failed. Source file missing.")
}