Fix occasionally failing temporary file and folder creation.

The previous implementation wasn't deterministic, instead counting
on the statistical improbability of generating an existing file/folder name.
This coupled with the fact that the random generator is weak (repeats
id within 2000 attempts) made it likely to generate an existing name,
leading to unexpected exception.

The solution is to use the Java provided functionality where possible
and retry until success otherwise.