Copy a file in Kotlin
This post will discuss how to copy the contents of the source file to the specified destination file in Kotlin. The solution will create the destination file if it doesn’t exist, otherwise, the file contents will be copied to the destination file and file attributes can be ignored.
1. Using Files.copy() function
To copy a file to a destination file, we can use Files.copy() function. By default, copying fails if the destination file already exists. Provide the REPLACE_EXISTING option to overcome this behavior.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.io.File import java.io.IOException import java.nio.file.Files import java.nio.file.StandardCopyOption private fun copyFile(src: File, dest: File) { Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING) } fun main() { val from = File("src.txt") val to = File("dest.txt") try { copyFile(from, to) println("File copied successfully.") } catch (ex: IOException) { ex.printStackTrace() } } |
If the source and destination point to the same file, the function completes without copying the file.
2. Using Stream
We can even write our own custom logic for copying a file using a FileInputStream and a FileOutputStream. To facilitate copying, we just need a buffer to copy all the bytes from one file to another. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException fun copyFile(src: File, dest: File) { FileInputStream(src).use { fis -> FileOutputStream(dest).use { os -> val buffer = ByteArray(1024) var len: Int while (fis.read(buffer).also { len = it } != -1) { os.write(buffer, 0, len) } } } } fun main() { val from = File("src.txt") val to = File("dest.txt") try { copyFile(from, to) println("File copied successfully.") } catch (ex: IOException) { ex.printStackTrace() } } |
Note that if the source and destination point to the same file, the file will be truncated.
3. Using FileChannel
We can use the FileChannel#transferFrom() function to transfer bytes from the source file to the destination file, as shown below. However, as with the previous approach, the contents of the file will be deleted if the source and destination are the same.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException fun copyFile(src: File, dest: File) { FileInputStream(src).channel.use { sourceChannel -> FileOutputStream(dest).channel .use { destChannel -> destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()) } } } fun main() { val from = File("src.txt") val to = File("dest.txt") try { copyFile(from, to) println("File copied successfully.") } catch (ex: IOException) { ex.printStackTrace() } } |
Note that copying a file is a non-atomic operation. i.e. the destination file may remain incomplete in case of an I/O error, power loss, process termination, etc.
That’s all about copying a file in java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)