Coming Up for Air

Easy File Copy in Kotlin

Sunday, Jun 24, 2018 |

Copying files in Java is, I think, header than it seems it should be. Typically, I see that done with, say, a ByteArrayInputStream and a ByteArrayOutputStream . Thanks to Kotlin's extension function capabilities, this operation is a one-liner:

File("/path/to/destination").writeBytes(File("/path/to/source").readBytes())

That could even be an extension function itself:

fun File.copyFile(dest: File): Unit = dest.writeBytes(this.readBytes())