private static String newUniquePath(File location){
  if (!location.exists()) {
    return location.getAbsolutePath();
  }
  String name=location.getName();
  File directory=location.getParentFile();
  int tries=0;
  while (true) {
    String candidateName=String.format("%s-%02d",name,tries);
    File candidateFile=new File(directory,candidateName);
    if (!candidateFile.exists()) {
      return candidateFile.getAbsolutePath();
    }
    tries++;
  }
}

private static String newUniquePath ( File location ) { if ( ! location . exists ( ) ) { return location . getAbsolutePath ( ) ; } String name = location . getName ( ) ; File directory = location . getParentFile ( ) ; int tries = 0 ; while ( true ) { String candidateName = String . format ( "%s-%02d" , name , tries ) ; File candidateFile = new File ( directory , candidateName ) ; if ( ! candidateFile . exists ( ) ) { return candidateFile . getAbsolutePath ( ) ; } tries ++ ; } }
returns a unique file path by appending numbers until a non - collision is found .
