Reputation: 352
If I use this method, and by chance there is a temporary file with the same name, the file is overwritten? I'm talking about an application that will generate many temporary files, for a long time.
Upvotes: 3
Views: 5758
Reputation: 12880
I'd suggest you to use a counter to create a new file name, if one already exists like this
File file = new File(filename);
for (int i = 0; file.exists(); i++) {
file = new File(filename + i);
}
// write the file now
Hope it gives an idea!
Upvotes: 0
Reputation: 201437
From the JavaDoc on createTempFile
(here) on the line labeled 2
,
Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
Edit
And the Returns section says
An abstract pathname denoting a newly-created empty file
And, it further states
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that: The file denoted by the returned abstract pathname did not exist before this method was invoked
Upvotes: 8