java - 如何列出所有大于给定参数的文件?

标签 java list file

我编写了这段代码,但不知道如何获取文件的 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/

相关文章:

c - 列表上的简单 merge_sort - 分析

java - 如何删除临时文件?

java - 解析带小数点的整数

java - Vert.x OutOfMemoryError 工作线程队列

python - 如何在使用 while 循环时将值 append 到字典中的列表?

javascript - Asp 网络核心 MVC : Passing a list of string as a parameter in javascript

java - 一段时间后重复调用webservice

java - postgresql 错误接近于 java

c - 学习 C 语言中文件的语法

php - Nginx + PHP7-FPM 内容类型为空 $FILES