瀏覽代碼

添加获取文件夹子文件数量的方法

tianboguang 2 年之前
父節點
當前提交
f0086f176b
共有 1 個文件被更改,包括 27 次插入0 次删除
  1. 27 0
      4dkankan-common-utils/src/main/java/com/fdkankan/common/util/FileUtils.java

+ 27 - 0
4dkankan-common-utils/src/main/java/com/fdkankan/common/util/FileUtils.java

@@ -8,6 +8,7 @@ import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import it.sauronsoftware.jave.*;
+import org.apache.commons.lang3.ObjectUtils;
 import org.bytedeco.javacpp.opencv_core;
 import org.bytedeco.javacv.FFmpegFrameGrabber;
 import org.bytedeco.javacv.Frame;
@@ -955,5 +956,31 @@ public class FileUtils {
         return null;
     }
 
+    /**
+     * 获取文件数量
+     * @param file 文件夹
+     * @return
+     */
+    public static long getSubFileNums(File file) {
+        long total = 0L;
+        try {
+            if (ObjectUtils.isEmpty(file) || !file.exists()) {
+                log.error("文件夹不存在");
+                return 0;
+            }
+            File[] subFiles = file.listFiles();
+            for (File subFile : subFiles) {
+                if (subFile.isFile()) {
+                    total++;
+                    continue;
+                }
+                total = total + getSubFileNums(subFile);
+            }
+        } catch (Exception e) {
+            log.error("获取文件数量失败!" , e);
+        }
+        return total;
+    }
+
 
 }