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);
}
}
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());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With