Kotlin Path.moveTo
Move an existing draft to a new name in the same directory.
A simple example
Path.moveTo
- Start with A file named a.txt
- Do this Rename it to b.txt
- Result b.txt holds the original contents
Syntax
draft.moveTo(destination)
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarPublish a signed attendance sheet
A class representative edits a draft until the names have been checked. Once the sheet is signed, rename it in the same workspace and confirm the final name is available for publication.
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.moveTo
import kotlin.io.path.writeText
import kotlin.io.path.name
fun main() {
val base = Files.createTempDirectory("lesson-")
try {
val draft = base.resolve("draft")
draft.writeText("signed")
val published = draft.moveTo(base.resolve("attendance.txt"))
if (Files.notExists(draft) && Files.isRegularFile(published)) println(published.name)
} finally {
base.toFile().deleteRecursively()
}
}
attendance.txt
After the successful rename the final entry identifies the published sheet.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. Rename the draft to its final name. What text can the next shift read under that name?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. Rename the draft to its final name. What text can the next shift read under that name?
What to watch for
Moving across storage volumes can have different failure and atomicity guarantees.