I want to add an index to a filename if the file already exists, so that I don\'t overwrite it.
Like if I have a file myfile.txt and same time myfile.
myfile.txt
myfile.
You might also benefit from using the apache commons-io library. It has some usefull file manipulation methods in class FileUtils and FilenameUtils.