使用 Java 7 新 IO 获取文件/目录大小

发布于 2024-12-02 01:08:16 字数 38 浏览 0 评论 0原文

如何使用 java 7 中的新 NIO 获取文件或目录的大小?

How can I get the size of a file or directory using the new NIO in java 7?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒绊 2024-12-09 01:08:16

使用 Files.size(Path) 获取文件的大小。

对于目录的大小(指其中包含的所有文件的大小),据我所知,您仍然需要手动递归。

Use Files.size(Path) to get the size of a file.

For the size of a directory (meaning the size of all files contained in it), you still need to recurse manually, as far as I know.

像你 2024-12-09 01:08:16

这是一个准备运行的示例,它还将跳过并记录它无法进入的目录。它使用 java.util.concurrent.atomic.AtomicLong 累积状态。

public static void main(String[] args) throws IOException {
    Path path = Paths.get("c:/");
    long size = getSize(path);
    System.out.println("size=" + size);
}

static long getSize(Path startPath) throws IOException {
    final AtomicLong size = new AtomicLong(0);

    Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) throws IOException {
            size.addAndGet(attrs.size());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc)
                throws IOException {
            // Skip folders that can't be traversed
            System.out.println("skipped: " + file + "e=" + exc);
            return FileVisitResult.CONTINUE;
        }
    });

    return size.get();
}

Here is a ready to run example that will also skip-and-log directories it can't enter. It uses java.util.concurrent.atomic.AtomicLong to accumulate state.

public static void main(String[] args) throws IOException {
    Path path = Paths.get("c:/");
    long size = getSize(path);
    System.out.println("size=" + size);
}

static long getSize(Path startPath) throws IOException {
    final AtomicLong size = new AtomicLong(0);

    Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file,
                BasicFileAttributes attrs) throws IOException {
            size.addAndGet(attrs.size());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc)
                throws IOException {
            // Skip folders that can't be traversed
            System.out.println("skipped: " + file + "e=" + exc);
            return FileVisitResult.CONTINUE;
        }
    });

    return size.get();
}
ι不睡觉的鱼゛ 2024-12-09 01:08:16
MutableLong size = new MutableLong();
Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                size.add(attrs.size());
            }
}

这将计算目录中所有文件的大小。但是,请注意,目录中的所有文件都需要是常规文件,因为 API 指定了 BasicFileAttributes 的大小方法:

“非常规文件的文件大小是特定于实现的,因此未指定。”

如果您偶然发现不受管制的文件,您将不得不不包含它的大小,或者返回一些未知的大小。您可以检查文件是否正常

BasicFileAttributes.isRegularFile()
MutableLong size = new MutableLong();
Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                size.add(attrs.size());
            }
}

This would calculate the size of all files in a directory. However, note that all files in the directory need to be regular files, as API specifies size method of BasicFileAttributes:

"The size of files that are not regular files is implementation specific and therefore unspecified."

If you stumble to unregulated file, you ll have either to not include it size, or return some unknown size. You can check if file is regular with

BasicFileAttributes.isRegularFile()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文