how to create multiple files with the same name in the same directory using java?

advertisements

As per my requirement,i need to upload a file into a specified directory,later after some modifications, i need to upload the same file into the same directory,here previous file should not be overridden means files must be saved in the same directory with same names(here i have one assumption,that , for example if my file is abc.txt, after modifications if i upload the modified file it can be saved as abc(1).txt ). how can i resolve my issue? can anybody assist me to come out from this issue.

Thanks in advance.


Use File.createNewFile() in a while loop. It will create the file if and only if the file does not exist. This is thread-safe, since the API guarantees atomicity.

Checking the existence of the file with File.exists() in a loop does not give you such guarantee.