简体   繁体   English

Java - 路径+大小中的子目录和文件列表?

[英]Java - list of subdirectories and files within a path + size?

For example, if I have a directory on my computer, c:\\test that contains the following directories and files: 例如,如果我的计算机上有一个目录,则c:\\ test包含以下目录和文件:

C:\\test\\foo\\a.dat (100kb) C:\\ test \\ foo \\ a.dat(100kb)

C:\\test\\foo\\b.dat (200kb) C:\\ test \\ foo \\ b.dat(200kb)

C:\\test\\foo\\another_dir\\jim.dat (500kb) C:\\ test \\ foo \\ another_dir \\ jim.dat(500kb)

C:\\test\\bar\\ball.jpg (5kb) C:\\ test \\ bar \\ ball.jpg(5kb)

C:\\test\\bar\\sam\\sam1.jpg (100kb) C:\\ test \\ bar \\ sam \\ sam1.jpg(100kb)

C:\\test\\bar\\sam\\sam2.jpg (300kb) C:\\ test \\ bar \\ sam \\ sam2.jpg(300kb)

C:\\test\\somefile.dat (700kb) C:\\ test \\ somefile.dat(700kb)

Running the command java ClassName c:\\test I want to have the output like that, sorting by size from largest to smallest ?: 运行命令java ClassName c:\\ test我希望得到类似的输出,按大小从最大到最小排序?:

DIR C:\\TEST\\FOO 800KB DIR C:\\ TEST \\ FOO 800KB

FILE C:\\TEST\\SOMEFILE.DAT 700KB 文件C:\\ TEST \\ SOMEFILE.DAT 700KB

DIR C:\\TEST\\BAR 405KB DIR C:\\ TEST \\ BAR 405KB

Path startPath = Paths.get("C:\\test");
Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
    System.out.println("Dir: " + dir.toString() + " "+getFolderSize(path()));
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("File: " + file.toString());    
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
return FileVisitResult.CONTINUE;
}
});

} catch (IOException e) {
e.printStackTrace();
}       

so far I got this part which can produce the output of all of the files and directories (which is not what I want) and without the size ? 到目前为止,我得到了这个部分,可以产生所有文件和目录的输出(这不是我想要的),没有大小? anybody with a help what to do next ? 任何人都有帮助下一步做什么?

THX! 谢谢!

I think you want something like this (output in bytes): 我想你想要这样的东西(输出以字节为单位):

import java.io.File;

public class FileSearcher {

    public static void main(String[] args) {

        File f = new File("C:\\test");
        FileSearcher.search(f);
    }

    public static void search(File f) {
        if(null == f) {
            return;
        }
        System.out.print("| " + f.getAbsolutePath());
        if(f.isFile()) {
            System.out.print(" (" +  f.length() + ")");
        } else if(f.isDirectory()) {
            File[] children = f.listFiles();
            if(null != children) {
                for(File c : children) {
                    search(c);
                }
            }
        }
        System.out.println("");
    }
}

Oh and if you happen to care about the human-friendly file sizes, something like this will convert them for you (just change f.length() to friendlyFileSize(f.length()): 哦,如果你碰巧关心人类友好的文件大小,这样的东西会为你转换它们(只需将f.length()更改为friendlyFileSize(f.length()):

public static String friendlyFileSize(long size) {
    String unit = "bytes";
    if(size > 1024) {
        size = size / 1024; 
        unit = "kb";
    }

    if(size > 1024) {
        size = size / 1024; 
        unit = "mb";
    }

    if(size > 1024) {
        size = size / 1024; 
        unit = "gb";
    }
    return size + " " + unit;
}

This solution sorts the results by size from largest to smallest using the format <file-path> (<file-size>kb) 此解决方案使用格式<file-path> (<file-size>kb)按大小从大到小对结果进行排序

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class du { 
    static class Pair {
        private String path;
        private Long size;
        public Pair(String path, Long size) { 
            this.path = path;
            this.size = size;
        }
        public String getPath() {
            return path;
        }
        public Long getSize() {
            return size;
        }
    }

    private static void diskUsage(File file, List<Pair> results) throws IOException {
        if (file.isDirectory()) {
            String[] subFolderPaths = file.list();
            for (String path : subFolderPaths) { 
                diskUsage(new File(path), results);
            }
        } else {
            results.add(new Pair(file.getAbsolutePath(), file.length()));
        }
    }

    public static void main(String... args) throws IOException {
        if (args.length != 1) {
            throw new IllegalArgumentException("Usage: du <folder-path>");
        }
        File folder = new File(args[0]);
        if (folder.isDirectory()) {
            List<Pair> results = new ArrayList<du.Pair>();
            diskUsage(folder, results);

            Collections.sort(results, new Comparator<Pair>() {
                @Override
                public int compare(Pair o1, Pair o2) {
                    return Long.compare(o2.getSize(), o1.getSize());
                }
            }); 

            for (Pair p : results) { 
                System.out.printf("%s (%dkb)\n", p.getPath(), p.getSize() / 1024);    
            }
        } else {
            System.out.printf("%s (%dkb)\n", folder.getAbsolutePath(), folder.length() / 1024);
        }
    }
}

You can iterate through each file in folder and call file.size() to get the size of file in bytes. 您可以遍历文件夹中的每个文件并调用file.size()以获取文件大小(以字节为单位)。 Keep summing it up and you will have the entire folder size. 继续总结,你将拥有整个文件夹大小。

For sorting, you can keep the file-name and size in a HashMap and then use a custom comparator to sort out the entire dictionary using Collections.sort 对于排序,您可以将文件名和大小保存在HashMap中,然后使用自定义比较器使用Collections.sort对整个字典进行排序

   import  java.io.File;
   import  java.util.ArrayList;
   import  java.util.Collection;
   import  java.util.Iterator;
   import  java.util.List;
   import  java.util.TreeMap;
   import  org.apache.commons.io.FileUtils;
/**
   <P>java SizeOrderAllFilesInDirXmpl</P>
 **/
public class SizeOrderAllFilesInDirXmpl  {
   public static final void main(String[] igno_red)  {
      File fDir = (new File("R:\\code\\xbn\\"));
      Collection<File> cllf = FileUtils.listFiles(fDir, (new String[]{"java"}), true);

      //Add all files to a Map, keyed by size.
      //It's actually a map of lists-of-files, to
      //allow multiple files that happen to have the
      //same length.

      TreeMap<Long,List<File>> tmFilesBySize = new TreeMap<Long,List<File>>();
      Iterator<File> itrf = cllf.iterator();
      while(itrf.hasNext())  {
         File f = itrf.next();
         Long LLen = f.length();
         if(!tmFilesBySize.containsKey(LLen))  {
            ArrayList<File> alf = new ArrayList<File>();
            alf.add(f);
            tmFilesBySize.put(LLen, alf);
         }  else  {
            tmFilesBySize.get(LLen).add(f);
         }
      }

      //Iterate backwards by key through the map. For each
      //List<File>, iterate through the files, printing out
      //its size and path.

      ArrayList<Long> alSize = new ArrayList<Long>(tmFilesBySize.keySet());
      for(int i=alSize.size() - 1; i >= 0; i--)  {
         itrf = tmFilesBySize.get(alSize.get(i)).iterator();
         while(itrf.hasNext())  {
            File f = itrf.next();
            System.out.println(f.length() + ": " + f.getPath());
         }
      }
   }
}

Output: 输出:

44271: R:\code\xbn\number\vx\XIFLengthInRange.java
39318: R:\code\xbn\text\ConsecutiveDups.java
34747: R:\code\xbn\z\iw\list\ify\IWListify.java
32224: R:\code\xbn\array\NonPArrayFromPrimitiveUtil.java
32130: R:\code\xbn\list\ify\earray\NewListifyPableArray.java
31842: R:\code\xbn\array\WrapperArrayUtil.java
30142: R:\code\xbn\text\regex\RegexReplacer.java
30129: R:\code\xbn\list\ListUtil.java
29116: R:\code\xbn\xcfu\XCFUCmdLineParams.java
27940: R:\code\xbn\array\helper\NewPrimitiveArrayHelper.java
27417: R:\code\xbn\z\iw\java\util\IWCollection.java
26807: R:\code\xbn\text\ProcessEscapedECs.java
...and so on...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM