Scala Files.isRegularFile and isDirectory
Distinguish regular files from directories.
A simple example
Files.isRegularFile and isDirectory
- Start with A regular file a.txt
- Do this Test whether it is a directory
- Result No
Syntax
Files.isRegularFile(upload)
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 LessonTell an uploaded file apart from its folder
A registration desk receives documents through an upload folder. The clerk needs to confirm that the submitted entry is a regular file and that the enclosing location is a directory before processing the batch.
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 upload = Files.createFile(base.resolve("upload"))
if (Files.isRegularFile(upload) && Files.isDirectory(base)) println("file and folder")
} finally {
val tree = Files.walk(base)
try tree.sorted(java.util.Comparator.reverseOrder[Path]()).forEach(p => Files.delete(p))
finally tree.close()
}
}
}
file and folder
The upload and its enclosing workspace have different entry types.
Locate a rehearsal score
The choir stores each score inside a season folder. A clerk creates an ordinary file at the given location. Which entry type should the status check report?
Locate a workshop handout
An instructor stores the current template in a class folder. A clerk creates an ordinary file at the given location. Which entry type should the status check report?
What to watch for
Status checks describe the instant of inspection. Handle failure at the subsequent open.