String (String searchDirectory, String preferredName, String extension) { for (int idx = 0; ; idx++) { String fileName = (idx > 0 ? preferredName + idx : preferredName) + extension; if (!Files.exists(Paths.get(searchDirectory, fileName))) { return fileName; } } }