Here you can find the source of findNextFileName(Path folder, String name)
public static String findNextFileName(Path folder, String name)
//package com.java2s; //License from project: Open Source License import java.nio.file.Files; import java.nio.file.Path; public class Main { public static String findNextFileName(Path folder, String name) { Path file = folder.resolve(name); if (!Files.exists(file)) { return name; }/*from w w w.j av a 2 s . c o m*/ String a = getNamePart(file); String b = getExtPart(file); if (b.length() > 0) { b = "." + b; } int i = 1; while (Files.exists(folder.resolve(a + i + b))) { i++; } return a + i + b; } public static String getNamePart(Path file) { String s = file.getFileName().toString(); return getNamePart(s); } public static String getNamePart(String filename) { int i = filename.lastIndexOf('.'); if (i == -1) { return filename; } return filename.substring(0, i); } public static String getExtPart(Path file) { String s = file.getFileName().toString(); return getExtPart(s); } public static String getExtPart(String filename) { int i = filename.lastIndexOf('.'); if (i == -1) { return ""; } return filename.substring(i + 1); } }