Here you can find the source of appendNumberToFilenameIfExists(String fileName, File directory)
public static File appendNumberToFilenameIfExists(String fileName, File directory)
//package com.java2s; import java.io.File; public class Main { public static File appendNumberToFilenameIfExists(String fileName, File directory) {/*from www . j a va 2 s.c o m*/ File newFile = new File(directory, fileName); int nextFile = 0; while (newFile.exists()) { nextFile++; newFile = new File(directory, fileName + " (" + Integer.toString(nextFile) + ")"); } return newFile; } }