示例#1
0
 /**
  * return a free file name. if file exist add a number as suffix.
  *
  * @param file
  * @return
  */
 public static synchronized File getFreeFileName(File file) {
   if (!file.exists()) {
     return file;
   }
   File folder = file.getParentFile();
   String newName = file.getName();
   String ext = StringHelper.getFileExtension(file.getName());
   String fileName = StringHelper.getFileNameWithoutExtension(file.getName());
   for (int i = 1; i < 999999; i++) {
     newName = fileName + "_" + i + '.' + ext;
     File newFile = new File(URLHelper.mergePath(folder.getAbsolutePath(), newName));
     if (!newFile.exists()) {
       return newFile;
     }
   }
   return null;
 }