Library contents

Scala Files.move

Move an existing draft to a new name in the same directory.

A simple example

Files.move

  1. Start with A file named a.txt
  2. Do this Rename it to b.txt
  3. Result b.txt holds the original contents
After a successful rename in the same directory the old name disappears.

Syntax

Scala syntax
Files.move(draft, destination)
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

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

Resultattendance.txt
Lesson.scala
import java.nio.file.Files
import java.nio.file.Path

object Lesson {
    def main(args: Array[String]): Unit = {
        val base = Files.createTempDirectory("lesson-")
        try {
            val draft = Files.writeString(base.resolve("draft"), "signed")
            val dest = Files.move(draft, base.resolve("attendance.txt"))
            if (Files.notExists(draft) && Files.isRegularFile(dest)) println(dest.getFileName)
        } finally {
            val tree = Files.walk(base)
            try tree.sorted(java.util.Comparator.reverseOrder[Path]()).forEach(p => Files.delete(p))
            finally tree.close()
        }
    }
}
Output
attendance.txt

After the successful rename the final entry identifies the published sheet.

Advertisements

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?

GivenFile contents “clay”
Worked resultclay

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?

GivenFile contents “ring bell”
Worked resultring bell

What to watch for

Moving across storage volumes can have different failure and atomicity guarantees.

Documentation for Files.move