Java 1.6,如何获取指定目录中所有内容使用的总字节数

标签 java

假设您有多个目录,它们都位于同一分区上。假设您想查找每个目录中的所有内容所使用的总字节数,有什么好方法吗?查看 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/

相关文章:

java - Maven Appassembler 插件 - stdout 和/或 stderr 重定向

java - 在大多数 JUnit 测试中抛出异常是否违反最佳实践?

java - 带有 StringEntity 的 HttpPost 具有特殊字符,如 ®,看到 ¿½` 而不是 ®

java - 从右到左设置 JFrame 方向!

java - 返回 ArrayList 的 SOAP 响应

java - Java 中的 NaN 常量魔法

java - hibernate 中的事务

java - 将一个元素与列表中的每个其他元素相加

java - 我可以通过 TCP java 套接字发送 @entity 吗?

java - 来自 Groovy 的带有参数的 invokeMethod