How to list all files larger than a given argument?

Bojan Petkovski :

I wrote this code and can't figure out how to get the length() of a file. I want to list all files that are bigger than 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);
    }
}
jeprubio :

The length() method to check the file size is of File, not String.

Try this:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=345071&siteId=1