MENU

Displays a list of files in the Java directory

TOC

JavadirectorywithinFile ListDisplaying File.listFiles()

Javaand the specifieddirectorywithinFile ListHere is a sample program that displays
File ListTo display the java.io.File The following methods of the class are used

  • list()... lists the files and directories contained in the specified directory. Array of String typeReturn by
  • listFiles()... lists the files and directories contained in the specified directory. Array of File typeReturn by


sample program

To check the sample program, we created a filelist directory and prepared the following files.

C:\filelist
    │ aaa.java
    │ bbb.java
    └─dir
           ccc.java
           ddd.java
           eee.jpg
public static void main(String[] args) {
    String path = "C:\filelist";
    File dir = new File(path);
    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        System.out.println((i + 1) + ": " + file);
    }
}

Execution Result

◆Output result

1: C:\filelist\aaa.java
2: C:\filelist\bbb.java
3: C:\filelist\dir

You were able to display a list of files and directories contained in "C:\filelist".
However, the list of files contained under "C:\filelist\dir" is not displayed.

java.io.File Class list(), ,listFiles() both do not seem to show up to subdirectory listings.
Next, we will introduce a program that easily displays and searches a list of files and directories, including subdirectories, through recursive processing.
java Recursively search for files

TOC