简体   繁体   English

我不知道如何更改我的程序以便我可以看到十个最大的文件

[英]I don´t know how to change my program so that i can see the ten largest files

I need to program some statistics, like Count files etc. But I have a problem with one of the tasks: Write code to identify the 10 largest Files.我需要编写一些统计数据,例如对文件进行计数等。但我在其中一项任务中遇到了问题:编写代码以识别 10 个最大的文件。

My Problem is that I don´t know how to get started.我的问题是我不知道如何开始。 I have code to get the largest file.我有代码来获取最大的文件。 But how i can get the ten largest files ?但是我怎样才能得到十个最大的文件呢? Here is one class that shows the largest file.这是一个显示最大文件的类。

@Override
public String getDescription() {
   return "find largest file";
}

@Override
public void printResults(List<File> files) {
    File largest = files.get(0);
    for (File f : files)
    if (f.length() > largest.length())
    largest = f;
    System.out.println("Largest file: " + largest.getName() + " (" + largest.length() + " bytes)");
}

You can sort the files by file size and get the first 10 elements.您可以按文件大小对文件进行排序并获取前 10 个元素。 Note that you will get strange results with this code if any of your files are really big:请注意,如果您的任何文件非常大,您将使用此代码得到奇怪的结果:

@Override
public void printResults(List<File> files) {
    files.sort((file1,file2) -> (int)(file1.length()-file2.length())); // This will NOT work correctly if any of the files are over 2.7 GB in size.
    List<File> largest = files.subList(0, files.size() > 10 ? 10 : files.size());
    System.out.println("Largest files:");
    for(File file : largest) {
        System.out.println(file.getName() + " (" + file.length() + " bytes)");
    }
}

You can use the Java Stream API to sort and limit the file list:您可以使用 Java Stream API 对文件列表进行排序和限制:

files.stream()
        .sorted(Comparator.comparingLong(File::length).reversed())
        .limit(10)
        .forEach(f -> System.out.println(f.getName() + " (" + f.length() + " bytes)"));

暂无
暂无

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

相关问题 我的程序遇到错误,我不知道如何解决 - My program caught an error I don't know how to fix 我不知道为什么我的程序给我花括号错误。 我怎样才能解决这个问题 ? 请帮忙 - I don't know why my program is giving me curly brace errors. How can I fix this ? Please help 如何更改我的代码,这样我就不必使用“break”? - How do I change my code, so that I don't have to use “break”? Java程序中出现ArrayIndexOutOfBoundsException错误,我不知道如何解决 - ArrayIndexOutOfBoundsException error in Java program, and I don't know how to fix it 我知道Java不支持通用数组,但是我不知道如何解决这个问题,因此它可以正常工作 - I know that generic arrays are not supported by Java, but I don't know how to fix this so it will work 如何隐藏我的API密钥,以便任何人反编译我的应用程序都看不到它? - How can I make my API key hidden so that anyone decompling my app wouldn't see it? 如何修复我的数组,以免出现 IndexOutOfBoundsException? - How can I fix my array so I don't have an IndexOutOfBoundsException? 如何绘制Java2D仿真的一部分,而该部分不会更改为图像/缓冲区,因此不必每次都重新绘制其图元? - How can I draw a portion of my Java2D simulation that doesn't change to an image/buffer so I don't have to redraw it's primitives each time? 我知道如何打开相机的按钮,但是我不知道如何更改拍摄照片的位置,我该怎么做? - I know how to make a button open the camera, but I don't how to change the location of the photos that are taken, how can I do this? 我不知道如何使用工具提示 - I don't know how to get my tooltips to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM