0

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.

Farzad
  • 1
  • 1

1 Answers1

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