访问目录的第一级并获取每个目录Java的大小 [英] visiting first level of directory and get Size of each directory Java

查看:216
本文介绍了访问目录的第一级并获取每个目录Java的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要C中的目录列表:并获取每个目录的大小. 我正在尝试使用此代码:

I need the list of the directories in the C: And get size for each one. I am trying with this code:

int[] count = {0};
try {
    Files.walkFileTree(Paths.get(dir.getPath()), new HashSet<FileVisitOption>(Arrays.asList(FileVisitOption.FOLLOW_LINKS)),
            Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file , BasicFileAttributes attrs) throws IOException {
                    System.out.printf("Visiting file %s\n", file);
                    ++count[0];

                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file , IOException e) throws IOException {
                    System.err.printf("Visiting failed for %s\n", file);

                    return FileVisitResult.SKIP_SUBTREE;
                }

                @Override
                public FileVisitResult preVisitDirectory(Path dir , BasicFileAttributes attrs) throws IOException {
                     System.out.printf("About to visit directory %s\n", dir);
                    return FileVisitResult.CONTINUE;
                }
            });
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

我的问题是,如果我恰好使用此方法,则会花费很多时间,因为它会访问每个文件!磁盘.我尝试使用FileVisitResult的differents选项,但无法获得所需的结果.我只需要第一级的磁盘空间. 预先感谢您的帮助.

The problem I have is that if I use exactly this, Takes a lot of time because it visits each file! of the disk. I tried with differents options for FileVisitResult but I can't get the desired result. I need only first level with space in the disk. Thanks in advance for any help.

推荐答案

文件夹没有自己的(很大)大小.通常,在讨论文件夹大小时,其含义是该文件夹及其子文件夹下所有文件的大小之和.对于C:\,此操作将花费非常长的时间"(尝试右键单击"Program Files"文件夹并单击属性).删除所有日志,然后重试.将代码花费的时间与Windows资源管理器计算该时间所花费的时间进行比较(同样,右键单击"Program Files"文件夹并单击属性).

Folders do not have a (significant) size of their own. Usually when a folder size is discussed, the meaning is the sum of sizes of all files under that folder and its sub-folders. This operation takes a "very long" time for C:\ (try right-clicking your Program Files folder and hitting properties). Remove all logs and try again. Compare the time it took your code, with the time it takes Windows Explorer to calculate that size (again, by right-clicking your Program Files folder and hitting properties).

在任何情况下,Apache的commons-io都有一个单行代码:

In any case, Apache's commons-io has a one-liner:

long size = FileUtils.sizeOfDirectory(folder);

Java-8还有另一种纯"的单行代码:

And Java-8 has another "pure" one-liner:

long size = Files.walk(Paths.get("C:\\"))
      .filter(p -> p.toFile().isFile())
      .mapToLong(p -> p.toFile().length())
      .sum();

这篇关于访问目录的第一级并获取每个目录Java的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆