copyTo

Platform and version requirements: JVM (1.5), JRE7 (1.5)
fun Path.copyTo(
    target: Path, 
    overwrite: Boolean = false
): Path

Copies a file or directory located by this path to the given target path.

Unlike File.copyTo, if some directories on the way to the target are missing, then they won't be created automatically. You can use the following approach to ensure that required intermediate directories are created:

sourcePath.copyTo(destinationPath.apply { parent?.createDirectories() })

If the target path already exists, this function will fail unless overwrite argument is set to true.

When overwrite is true and target is a directory, it is replaced only if it is empty.

If this path is a directory, it is copied without its content, i.e. an empty target directory is created. If you want to copy directory including its contents, use copyRecursively.

The operation doesn't preserve copied file attributes such as creation/modification date, permissions, etc.

Parameters

overwrite - true if destination overwrite is allowed.

Exceptions

NoSuchFileException - if the source path doesn't exist.

FileAlreadyExistsException - if the destination path already exists and overwrite argument is set to false.

DirectoryNotEmptyException - if the destination path point to an existing directory and overwrite argument is true, when the directory being replaced is not empty.

IOException - if any errors occur while copying.

Return the target path.

See Also

Files.copy

Platform and version requirements: JVM (1.5), JRE7 (1.5)
fun Path.copyTo(
    target: Path, 
    vararg options: CopyOption
): Path

Copies a file or directory located by this path to the given target path.

Unlike File.copyTo, if some directories on the way to the target are missing, then they won't be created automatically. You can use the following approach to ensure that required intermediate directories are created:

sourcePath.copyTo(destinationPath.apply { parent?.createDirectories() })

If the target path already exists, this function will fail unless the REPLACE_EXISTING is option is used.

When REPLACE_EXISTING is used and target is a directory, it is replaced only if it is empty.

If this path is a directory, it is copied without its content, i.e. an empty target directory is created. If you want to copy a directory including its contents, use copyRecursively.

The operation doesn't preserve copied file attributes such as creation/modification date, permissions, etc. unless COPY_ATTRIBUTES is used.

Parameters

options - options to control how the path is copied.

Exceptions

NoSuchFileException - if the source path doesn't exist.

FileAlreadyExistsException - if the destination path already exists and REPLACE_EXISTING is not used.

DirectoryNotEmptyException - if the destination path point to an existing directory and REPLACE_EXISTING is used, when the directory being replaced is not empty.

IOException - if any errors occur while copying.

Return the target path.

See Also

Files.copy

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io.path/java.nio.file.-path/copy-to.html