I want to Copy a file from my res directory to internal storage and delete that file after closing my application with Kotlin. There are some code in Java but my goal is reaching that with Kotlin programing. How can I do that? I searched The web but there are not Good tutorial for this.
Asked
Active
Viewed 1,374 times
1 Answers
1
Read the file content from resources like:
val fileContent = MySpec::class.java.getResource("/myFiles/file.html").readText()
You can use this copyTo
in Kotlin like:
fun InputStream.copyTo(
out: OutputStream,
bufferSize: Int = DEFAULT_BUFFER_SIZE
): Long
This will Copies this stream to the given output stream, returning the number of bytes copied
Note: It is the caller's responsibility to close both of these resources. Source: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-input-stream/copy-to.html
Delete it using:
val file = File(yourFIlePath)
file.absoluteFile.delete()

Shailendra Madda
- 20,649
- 15
- 100
- 138
-
Thanks for answering. But it's not my answer. I don't want create a file. I have a file that i copied in my res folder and i wants to Copy that file to internal storage. – Farzad Oct 14 '20 at 12:53
-
Java or Koltin but the syntax is different but the logic is the same ;) – Shailendra Madda Oct 14 '20 at 13:07
-
Thanks for your help but I'm trying all this codes. But not worked in Kotlin project. – Farzad Oct 14 '20 at 13:10
-