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

时间:2014-01-02 18:40:53

标签: java file size directory

例如,如果我的计算机上有一个目录,则c:\ test包含以下内容 目录和文件:

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

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

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

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

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

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

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

运行命令java ClassName c:\ test我希望得到类似的输出,按大小从最大到最小排序?:

DIR C:\ TEST \ FOO 800KB

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

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();
}       

到目前为止,我得到的这部分可以产生所有文件和目录的输出(这不是我想要的)并且没有大小?任何帮助下一步做什么的人?

THX!

4 个答案:

答案 0 :(得分:1)

我认为你想要这样的东西(输出以字节为单位):

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("");
    }
}

哦,如果你碰巧关心人类友好的文件大小,这样的东西会为你转换它们(只需将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;
}

答案 1 :(得分:1)

此解决方案使用格式按大小从大到小对结果进行排序 <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);
        }
    }
}

答案 2 :(得分:0)

您可以遍历文件夹中的每个文件并调用file.size()以获取文件大小(以字节为单位)。继续总结,你将拥有整个文件夹大小。

对于排序,您可以将文件名和大小保存在HashMap中,然后使用自定义比较器使用Collections.sort

整理整个字典

答案 3 :(得分:0)

   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());
         }
      }
   }
}

输出:

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...