Kotlin Files.Move

The java.nio.files.Files class also has a move method that is used to move a file (or empty folder) from one location to another on a file system. Here is an example Kotlin program that demonstrates a move operation.

package ch9.files

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption

private fun Path.exists() : Boolean = Files.exists(this)

private fun Path.isFile() : Boolean = !Files.isDirectory(this)

fun Path.move(dest : Path, overwrite : Boolean = false) : Boolean {
    return if(isFile()){
                //Perform the move operation. REPLACE_EXISTING is needed for
                //replacing a file
                Files.move(this, dest, StandardCopyOption.REPLACE_EXISTING)
            } else {
        } else {
            //Perform the move operation
            Files.move(this, dest)
    } else {

private fun prompt(msg : String) : String {
    print("$msg => ")
    return readLine() ?: ""

fun main(args : Array<String>){
    when (args.size){
        2 -> {
            val src = Paths.get(args[0])
            val dest = Paths.get(args[1])

            if (dest.exists()){
                val answer = prompt("File exists! Replace (y/n)?")
                if(answer.toLowerCase() == "y"){
                    src.move(dest, true)
                    println("Moving complete")
                } else {
            } else {
                println("Moving complete")
        else -> {
            println("Usage: src dest")


Moving a file using the Files class is shown on lines 18 and 25. The move() method is a static method that accepts the source path, destination path, and optionally a StandardCopyOption enumeration. The StandardCopyOption.REPLACE_EXISTING is used when the destination file exists. If we forget it, the operation will throw an execption. REPLACE_EXISTING isn’t needed when moving a file to a path that doesn’t previously exist. In this case, the copy operation is simply performed without provided a StandardCopyOption.

The demonstration program wraps the move operation in an extension function for the Path interface (lines 12-30). This allows use to call move() on a Path object rather (which seems more intuitive) rather than passing a Path object into the Files.move() method directly. We also have two other extension functions used by the program.

The exist() function (line 8) wraps Files.exists() so that we can call exists() directly on a Path object. Likewise, we have an isFile() extension function (line 10) that wraps Files.isDirectory() so that we can test if a Path is a file or not directly. The move() extension function uses isFile() and exists() to determine if it can proceed with the move operation.

The main function also uses the extension function. Line 44 tests if the file exists and askes the user if they wish to overwrite the file. If the user answers (y) for yes, the move() extension function is called on line 47 with overwrite set to true. Line 53 is used when the file doesn’t already exists, in which case the move() extension function is used with overwrite set to false (the default value).




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s