Get control of the name of the temporary file created in Java
When creating tempfile, is there any way to control the random number attached to the file name?
Solution
It seems that the API does not provide this directly However, you can view the file Createtempfile () source code to understand how it is implemented, and then implement the required methods yourself
Basically, createtempfile () creates a file object with the expected file name, and then uses filesystem Createfileexclusively() to create the file If the file already exists, this method returns false, in which case the file name is modified (by using a different random number) and the creation is retried
You can follow the same method, but note that filesystem is a package private class, so you can't use it in your own methods Please use file Createnewfile() creates the file atomically This method also returns false if the file already exists, so you can use it in a similar loop, for example, createtempfile() uses the createfileexclusively() method