I like the multiple attempts at creating a unique name but even this solution does not rule out a race condition. Another process can slip in after the test for exists()
and the if(newTempDir.mkdirs())
method invocation. I have no idea how to completely make this safe without resorting to native code, which I presume is what's buried inside File.createTempFile()
.