Scala Files.createDirectories
Create a nested directory tree and verify the final directory.
A simple example
Files.createDirectories
- Start with An empty practice folder
- Do this Create a/b including parents
- Result Both directories exist
Syntax
Files.createDirectories(week)
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 LessonPrepare nested hand-in folders
A department creates a hand-in folder for each class and week. The job runs again if the first delivery is interrupted, so existing directories should remain usable on a second run.
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 week = base.resolve("class/week1")
(1 to 2).foreach(_ => Files.createDirectories(week))
if (Files.isDirectory(week)) println("ready")
} finally {
val tree = Files.walk(base)
try tree.sorted(java.util.Comparator.reverseOrder[Path]()).forEach(p => Files.delete(p))
finally tree.close()
}
}
}
ready
The second attempt finds the same directory and the application can use it.
Locate a rehearsal score
The choir stores each score inside a season folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?
Locate a workshop handout
An instructor stores the current template in a class folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?
What to watch for
Handle a regular file occupying any required directory name as an error.