copyTo

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

(source)

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

JVM
JRE7
1.5
fun Path.copyTo(
    target: Path,
    vararg options: CopyOption
): Path

(source)

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