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

使用 File 类的 list 方法来输出指定目录下的所有文件

完整代码

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);
            }
        }
    }
}

结果输出

file
login.jsp
index.xml
test.txt
helloWorld.java
helloWorld.class
src

在这里插入图片描述

发布了827 篇原创文章 · 获赞 112 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45743799/article/details/105216861