Ver código fonte

代码优化

JustryDeng 5 anos atrás
pai
commit
291c58e918

+ 16 - 10
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/IOUtil.java

@@ -62,13 +62,16 @@ public final class IOUtil {
                     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") + ".");
+                        Arrays.stream(Objects.requireNonNull(parentFile.getParentFile().listFiles()))
+                                .filter(file -> file.getName().equalsIgnoreCase(parentFile.getName())).findFirst()
+                                .ifPresent(conflictFile -> {
+                                    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) {
@@ -214,7 +217,10 @@ public final class IOUtil {
         if (!dirOrFile.exists()) {
             return;
         }
-        if (mode != 0 && mode != 1 && mode != 2) {
+        int fileAndDirMode = 0;
+        int onlyFileMode = 1;
+        int onlyDirMode = 2;
+        if (mode != fileAndDirMode && mode != onlyFileMode && mode != onlyDirMode) {
             throw new IllegalArgumentException("mode [" + mode + "] is non-supported. 0,1,2is only support.");
         }
         if (dirOrFile.isDirectory()) {
@@ -224,11 +230,11 @@ public final class IOUtil {
                     listFile(f, fileContainer, mode);
                 }
             }
-            if (mode == 0 || mode == 2) {
+            if (mode == fileAndDirMode || mode == onlyDirMode) {
                 fileContainer.add(dirOrFile);
             }
         } else {
-            if (mode == 0 || mode == 1) {
+            if (mode == fileAndDirMode || mode == onlyFileMode) {
                 fileContainer.add(dirOrFile);
             }
         }