• java获取指定文件夹下文件个数和文件总大小(使用递归方式和循环方式分别计算)


     1 import java.io.File;
     2 import java.util.Arrays;
     3 import java.util.Stack;
     4 
     5 public class TestStack {
     6     public static void main(String[] args) {
     7         String path = "/mnt/dev/test";
     8         long start = System.currentTimeMillis();
     9         long[] result = loop(path);         //循环方式 五次分别耗时  174 ms, 248 ms, 293 ms, 185 ms, 173 ms
    10 //        long[] result = recursion(path);   //递归方式 五次分别耗时  197 ms, 226 ms, 207 ms, 212 ms, 294 ms
    11         System.out.println(Arrays.toString(result));
    12         long end = System.currentTimeMillis();
    13         System.out.println("total time :" + (end - start) + " ms");
    14     }
    15 
    16     /**
    17      * 循环 获取指定目录下文件个数和文件大小
    18      *
    19      * @param path
    20      * @return long[fileCount, dataSize(byte)]
    21      */
    22     public static long[] loop(String path) {
    23         File file = new File(path);
    24         Stack<File> stack = new Stack<File>();
    25         stack.push(file);
    26         long fileCount = 0;
    27         long dataSize = 0;
    28         while (!stack.isEmpty()) {
    29             File child = stack.pop();
    30             if (child.isDirectory()) {
    31                 // 排除隐藏目录
    32                 if (!child.isHidden() && !child.getName().startsWith(".")) {
    33                     for (File f : child.listFiles()) stack.push(f);
    34                 }
    35             } else if (child.isFile()) {
    36                 // 排除隐藏文件
    37                 if (!child.isHidden() && !child.getName().startsWith(".")) {
    38                     fileCount += 1;
    39                     dataSize += child.length();
    40                 }
    41             }
    42         }
    43         return new long[]{fileCount, dataSize};
    44     }
    45 
    46     /**
    47      * 递归 获取指定目录下文件个数和文件大小
    48      *
    49      * @param path
    50      * @return long[fileCount, dataSize(byte)] byte
    51      */
    52     public static long[] recursion(String path) {
    53         File files = new File(path);
    54         if (files.isFile()) {
    55             // 排除隐藏文件或目录
    56             if (!files.isHidden() && !files.getName().startsWith(".")) {
    57                 return new long[]{1, files.length()};
    58             } else {
    59                 return new long[]{0, 0};
    60             }
    61         }
    62         long[] result = new long[]{0, 0};
    63         for (File file : files.listFiles()) {
    64             if (!file.isHidden() && !file.getName().startsWith(".")) {
    65                 long[] longs = file.isDirectory() ? recursion(file.getAbsolutePath()) : new long[]{1, file.length()};
    66                 result[0] += longs[0];
    67                 result[1] += longs[1];
    68             }
    69         }
    70         return result;
    71     }
    72 }
  • 相关阅读:
    关于HTTP协议,一篇就够了
    jvm在什么情况下会执行GC
    为什么我们做分布式使用Redis?
    linux入门系列
    linux学习笔记-13.进程控制
    linux学习笔记-12.输入输出重定向及管道
    app获取自己的签名
    安卓给微信公众号发消息
    微信扫码下载apk
    设备通道开启关闭状态
  • 原文地址:https://www.cnblogs.com/dashuai-wang/p/14922008.html
Copyright © 2020-2023  润新知