/**
* 创建同名文件名称拼接(数字)
*
* @param path 需要创建的目录
* @return
*/
public static String recursionMkdirsFile(String path) {
String newPath = path;
for (int i = 1; i < 10000; path = newPath + "(" + i++ + ")") {
File file = new File(path);
if (file.exists()) {
} else {
file.mkdirs();
break;
}
}
return path;
}
/**
* 同名文件名称拼接新名称
*
* @param path 文件目录
* @param fileName 文件名称
* @param index 索引
* @return
*/
public static String recursionMkdirsEFileName(String path, String fileName, int index) {
Path directoryPath = Paths.get(path);
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directoryPath)) {
for (Path path2 : directoryStream) {
String pathName = path2.getFileName().toString();
if (pathName.equalsIgnoreCase(fileName)) {
index++;
int i = index - 1;
if (index > 0) fileName = fileName.replaceAll("\\(" + i + "\\)", "");
fileName = recursionMkdirsEFileName(path, FileUtil.getName(fileName) + "(" + index + ")"
+ FileUtil.getSuffix(fileName), index);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}