我写了这段代码,但不知道如何获取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)
检查文件大小的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)