, / . , !
public static File getValidFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return file;
} else {
int extAt = filePath.lastIndexOf('.');
String filePart = filePath;
String ext = "";
if(extAt > 0) {
filePart = filePath.substring(0, extAt);
ext = filePath.substring(extAt);
}
if (filePart.endsWith(")")) {
try {
int countStarts = filePart.lastIndexOf('(');
int countEnds = filePart.lastIndexOf(')');
int count = Integer.valueOf(filePart.substring(countStarts + 1, countEnds));
filePath = filePart.substring(0, countStarts + 1) + ++count + ")" + ext;
} catch (NumberFormatException | StringIndexOutOfBoundsException ex) {
filePath = filePart + "(1)" + ext;
}
} else {
filePath = filePart + "(1)" + ext;
}
return getValidFile(filePath);
}
}