Here you can find the source of getUniqueFileInDirectory(Path dir, String name)
public static Path getUniqueFileInDirectory(Path dir, String name)
//package com.java2s; //License from project: Apache License import java.nio.file.Files; import java.nio.file.Path; public class Main { public static Path getUniqueFileInDirectory(Path dir, String name) { int extension = name.lastIndexOf('.'); return extension <= 0 || extension >= name.length() - 1 ? getUniqueFileInDirectory(dir, name, "") : getUniqueFileInDirectory(dir, name.substring(0, extension), name.substring(extension)); }/*from www . j a va 2 s . c o m*/ public static Path getUniqueFileInDirectory(Path dir, String name, String extension) { Path file = dir.resolve(name + extension); if (Files.exists(file)) { file = dir.resolve(name + " copy" + extension); for (int i = 2; Files.exists(file); i++) { file = dir.resolve(String.format("%s copy %d%s", name, i, extension)); } } return file; } }