IOUtils { public static File findNewNameThatDoesNotExist(File output) throws IOException { File newNamedOutput; int count = 1; int maxTries = 100; String basename = FilenameUtils.getBaseName(output.getName()); String extension = FilenameUtils.getExtension(output.getName()); do { String newName = String.format("%s(%d).%s", basename, count, extension); newNamedOutput = new File(output.getParent(), newName); count++; } while (count < maxTries && newNamedOutput.exists()); if (newNamedOutput.exists()) { LOG.warn("Unable to generate a new filename that does not exist, path was {}", output); throw new IOException( String.format("Unable to generate a new filename that does not exist, path was %s", output)); } return newNamedOutput; } private  IOUtils(); static File createTemporaryBuffer(TaskOutput taskOut); static File createTemporaryBuffer(); static File createTemporaryBuffer(String extension); static File createTemporaryBufferWithName(String filename); static File createTemporaryFolder(); static File findNewNameThatDoesNotExist(File output); static String shortenFilename(String name); static String toSafeFilename(String input); static String toStrictFilename(String input);  }
@Test public void testFindNewNameThatDoesNotExist() throws Exception { File file = folder.newFile("chuck.norris"); assertEquals("chuck(1).norris", IOUtils.findNewNameThatDoesNotExist(file).getName()); folder.newFile("chuck(1).norris"); assertEquals("chuck(2).norris", IOUtils.findNewNameThatDoesNotExist(file).getName()); }