如何列出大于给定参数的所有文件?

Boj*_*ski 3 java file list

我写了这段代码,但不知道如何获取length()文件的。我想列出所有大于 50KB 的文件。

public static void main(String[] args) throws IOException {

    File f = new File(".");
    int KB = 1024;
    String[] files = f.list();

    for (String string : files) {
        if (f.length() > 50*KB)
        System.out.println(string);
    }
}
Run Code Online (Sandbox Code Playgroud)

jep*_*bio 5

检查文件大小的length()方法是File,而不是String。

尝试这个:

public static void main(String[] args) throws IOException {
    File f = new File(".");
    int KB = 1024;
    File[] allSubFiles = f.listFiles();
    for (File file : allSubFiles) {
        if (file.length() > 50 * KB) {
            System.out.println(file.getName());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)