假设您有多个目录,它们都位于同一分区上。假设您想查找每个目录中的所有内容所使用的总字节数,有什么好方法吗?查看 Java File 和 Apache FileUtils,我只能获取有关分区的磁盘使用信息,但我需要的是能够将其限制到指定目录。
Example:
Partition X: 100GB
Root directories:
/x/1/
/x/2/
/x/3/
Deeper directories can exist under each root directory:
/x/1/files/a/deeper/path/test.txt
/x/2/files/test.txt
/x/3/files/path/to/files/test.txt
Goal:
long byteSumForX1 = ??
假设上述 3 个路径存在于分区 x 下,我希望能够找到上面指定的每个根目录使用的总字节数。总数需要递归并包括根目录中的所有文件和目录
有什么想法吗?我更愿意为此利用一些现有的库,如果存在的话(假设/希望它存在)
最佳答案
最简单的解决方案是使用 sizeOfDirectory()方法,在 Apache Commons 的 FileUtils
类中找到。引用 javadocs:
Counts the size of a directory recursively (sum of the length of all files).
Parameters: directory - directory to inspect, must not be null
Returns: size of directory in bytes, 0 if directory is security restricted
关于Java 1.6,如何获取指定目录中所有内容使用的总字节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863225/