Java 实例 - 在指定目录中查找文件

3295 1
LZH实名认证 2022-3-20 15:08:29 | 显示全部楼层 |阅读模式
以下实例演示了使用 File 类的 dir.list() 方法在指定目录中查找所有文件列表:

Main.java 文件
import java.io.File;

public class Main {
    public static void main(String[] argv) throws Exception {
        File dir = new File("../java");
        String[] children = dir.list();
        if (children == null) {
            System.out.println("该目录不存在");
        }
        else {
            for (int i = 0; i < children.length; i++) {
                String filename = children[i];
                System.out.println(filename);
            }
        }
    }
}
以上代码运行输出结果为:

Car.class
FileUtil.class
FileUtil.java
HelloWorld.class
HelloWorld.java
HelloWorldDebug.class
HelloWorldDebug.java
……
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行