Java 中列出目录中的所有文件

13 May 2025 | 5 分钟阅读

在本节中,我们将学习如何列出目录中存在的所有文件。请注意,一个目录可能包含子目录,而该子目录可能包含一些文件。我们也必须列出这些文件,因为这些文件也属于主目录。但是,为了简单起见,我们将首先讨论不存在子目录的情况。

没有子目录的目录

这意味着一个文件夹只包含文件。下面提到了打印目录中文件的步骤。

步骤 1:为目录创建一个 File 对象。

步骤 2:获取该目录的文件数组。

步骤 3:递归地,逐个打印文件名(带扩展名)。

实施

以下代码打印名为 Documents 的文件夹中提到的文件名。以下快照显示了名为 Documents 的文件夹中存在的文件。

List All Files in a Directory in Java

文件名: DisplayFileExample.java

输出

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Displaying Files from the directory: E:\Documents
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
30004399657726 report 2.pdf
30004399657726 report.pdf
5010XXXXXX8778-13 Mar 2017 TO 12 Apr 2017.pdf
a b c.txt
asp.net tutorial.pdf
coding Standards.pdf
CS121Lec01.pdf
Databases for beginners.pdf
DBMS ME CS (gate2016.info).pdf
lec1.pdf
New Text Document.txt
Programming Interview Questions.docx
Rk - Database Management Systems 3rd Edition.pdf
slip201611605845.pdf
slip201611605845_2.pdf
Task 1.pdf
The Normal Forms2.ppt

带有子目录的目录

这意味着一个文件夹既包含文件又包含子文件夹。下面提到了打印目录及其子目录中文件的步骤。

步骤 1:为目录创建一个 File 对象。

步骤 2:获取该目录的文件和子目录的数组。

步骤 3:如果 array[j] 是一个文件,则显示文件名并递归地转到 array[j] 的下一个元素。

步骤 4:如果 array[j] 是一个目录,则显示目录名,然后从步骤 2 开始重复。

实施

我们已经看到了名为 Documents 的文件夹的内容。现在,我们在 Documents 中创建一些文件夹并在其中存储多个文件。请观察以下快照。

List All Files in a Directory in Java

上面的快照显示 Documents 目录有 3 个子目录,分别名为(New folder - 1、New folder - 2、New folder - 3)。

以下快照显示了名为 New folder - 1 的文件夹的内容

List All Files in a Directory in Java

以下快照显示了名为 New folder - 2 的文件夹的内容

List All Files in a Directory in Java

以下快照显示了名为 New folder - 3 的文件夹的内容

List All Files in a Directory in Java

创建文件夹后,我们可以根据上述步骤编写代码。

文件名: DisplayFileExample1.java

输出

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Displaying Files from the directory: E:\Documents
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
30004399657726 report 2.pdf
30004399657726 report.pdf
5010XXXXXX8778-13 Mar 2017 TO 12 Apr 2017.pdf
a b c.txt
asp.net tutorial.pdf
coding Standards.pdf
CS121Lec01.pdf
Databases for beginners.pdf
DBMS ME CS (gate2016.info).pdf
lec1.pdf
[New folder  - 1]
        How to call private method from another class in java.docx
        Java CardLayout.docx
        Java GridBagLayout.docx
        Java SpringLayout.docx
        ordinal number in java.docx
        tetrahedral number in java.docx
[New folder  - 2]
        output onlinepngtools (13).png
        output onlinepngtools (14).png
        output onlinepngtools (15).png
        output onlinepngtools (16).png
        output onlinepngtools (17).png
        output onlinepngtools (18).png
[New folder  - 3]
        Java Regular Expressions.docx
        n1 (7).png
        n2 (3).png
        n3.png
        n4.png
        String vs StringBuffer.docx
        StringBuffer in Java.docx
        StringBuffer vs StringBuilder.docx
        StringBuilder in Java.docx
New Text Document.txt
Programming Interview Questions.docx
Rk - Database Management Systems 3rd Edition.pdf
slip201611605845.pdf
slip201611605845_2.pdf
Task 1.pdf
The Normal Forms2.ppt