|
|
@@ -53,9 +53,28 @@ public final class IOUtil {
|
|
|
}
|
|
|
|
|
|
if (createIfNecessary && !destFile.exists()) {
|
|
|
- if (!destFile.getParentFile().exists()) {
|
|
|
- //noinspection ResultOfMethodCallIgnored
|
|
|
- destFile.getParentFile().mkdirs();
|
|
|
+ File parentFile = destFile.getParentFile();
|
|
|
+ if (!parentFile.exists() || !parentFile.isDirectory()) {
|
|
|
+ /*
|
|
|
+ * 进入此if,即代表parentFile存在,且为file, 而我们又需要创建一个同名的文件夹。
|
|
|
+ * 如果系统不支持创建与文件同名(大小写不敏感)的文件夹的话,那么创建结果为false
|
|
|
+ */
|
|
|
+ boolean mkdirs = parentFile.mkdirs();
|
|
|
+ if (!mkdirs) {
|
|
|
+ // step0. 将与与文件夹名冲突的文件重命名为:原文件名_时间戳
|
|
|
+ File conflictFile = Arrays.stream(Objects.requireNonNull(parentFile.getParentFile().listFiles()))
|
|
|
+ .filter(file -> file.getName().equalsIgnoreCase(parentFile.getName())).findFirst().get();
|
|
|
+ String renameFilePath = conflictFile.getAbsolutePath() + "_" + System.currentTimeMillis();
|
|
|
+ boolean renameResult = conflictFile.renameTo(new File(renameFilePath));
|
|
|
+ Logger.warn(IOUtil.class,
|
|
|
+ "rename file [" + conflictFile.getAbsolutePath() + "] to ["
|
|
|
+ + renameFilePath + "] " + (renameResult ? "success" : "fail") + ".");
|
|
|
+ // step1. 再次创建文件夹
|
|
|
+ mkdirs = parentFile.mkdirs();
|
|
|
+ if (!mkdirs) {
|
|
|
+ Logger.warn(IOUtil.class, "create dir [" + parentFile.getAbsolutePath() + "] fail.");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
destFile.createNewFile();
|