copyTo

inline 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.

Since Kotlin

1.5

Return

the target path.

Parameters

overwrite

true if destination overwrite is allowed.

See also

Throws

if the source path doesn't exist.

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

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

if any errors occur while copying.


inline 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.

Since Kotlin

1.5

Return

the target path.

Parameters

options

options to control how the path is copied.

See also

Throws

if the source path doesn't exist.

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

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

if any errors occur while copying.