diff --git a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt index 553261cf248..d61b9a2d0f3 100644 --- a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt +++ b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt @@ -184,15 +184,10 @@ public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int } if (target.exists()) { - val stillExists = if (!overwrite) true else !target.delete() - - if (stillExists) { - throw FileAlreadyExistsException( - file = this, - other = target, - reason = "The destination file already exists." - ) - } + if (!overwrite) + throw FileAlreadyExistsException(file = this, other = target, reason = "The destination file already exists.") + else if (!target.delete()) + throw FileAlreadyExistsException(file = this, other = target, reason = "Tried to overwrite the destination, but failed to delete it.") } if (this.isDirectory) {