Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all files larger than a given argument?

Tags:

java

file

list

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);
    }
}
like image 751
Bojan Petkovski Avatar asked Dec 03 '25 03:12

Bojan Petkovski


1 Answers

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());
        }
    }
}
like image 137
jeprubio Avatar answered Dec 05 '25 18:12

jeprubio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!