Reputation: 95
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);
}
}
Upvotes: 3
Views: 481
Reputation: 18012
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());
}
}
}
Upvotes: 5
Reputation: 4723
File
has a method File#length
which gives you the amount of bytes of a file object. You need to divide that number by 1024 to get the amount of kilobytes.
Assuming a given array of files you can do this:
File[] files = getFiles();
for (File f : files) {
if (f.length() / 1024 > 50) {
System.out.println(f.getName());
}
}
Upvotes: 3