Java 实例 - 输出指定目录下的所有文件

3287 1
LZH实名认证 2022-3-20 15:15:46 | 显示全部楼层 |阅读模式
以下实例演示了如何使用 File 类的 list 方法来输出指定目录下的所有文件:

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

class Main {
    public static void main(String[] args) {
        File dir = new File("C:");
        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);
            }
        }
    }
}
以上代码运行输出结果为:

build
build.xml
destnfile
detnfile
filename
manifest.mf
nbproject
outfilename
src
srcfile
test:):):)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

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