Groovy FileAlreadyExistsException
An exclusive creation on the local filesystem rejects a name already used by another shift.
A simple example
FileAlreadyExistsException
- Start with A file named a.txt already exists
- Do this Try exclusive creation at the same name
- Result Creation fails
Syntax
Files.createFile(report)
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovySave a report without replacing the first copy
Two shifts try to save their report under the same filename. The second shift needs to learn that the name is taken while the first shift's report stays available.
import java.nio.file.Files
import java.nio.file.FileAlreadyExistsException
def report = Files.createTempFile("shift-", ".txt")
try {
Files.createFile(report)
} catch (FileAlreadyExistsException problem) {
println("Choose a new report name")
} finally {
Files.deleteIfExists(report)
}
Choose a new report name
The exclusive create sees the existing report. Its earlier contents are preserved.
Issue the first parcel claim
The counter opens a new workspace for today’s pickup. Create the claim file exclusively so that an existing claim stays intact.
Preserve a previous reservation
The second counter tries a name already reserved by the first counter. Create the claim file exclusively so that an existing claim stays intact.
What to watch for
The specific exception depends on the provider. The example creates and cleans up its own temporary report.