我编写了这段代码,但不知道如何获取文件的 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);
}
}
最佳答案
检查文件大小的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());
}
}
}
关于java - 如何列出所有大于给定参数的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790017/