Home >Java >javaTutorial >How to List All Filenames in a Java Directory?
Listing All Filenames in a Directory
Question:
In Java, how do you obtain a list containing the complete filenames of all files within a particular directory?
Sample Data:
Consider a directory with the following files:
Desired Output:
An ArrayList with the values [000, 012, 013] representing the file names.
Solution:
To retrieve the filenames of all files in a directory, utilize the following steps:
Example Code:
<code class="java">File folder = new File("your/path"); File[] listOfFiles = folder.listFiles(); if (listOfFiles != null) { for (File file : listOfFiles) { if (file.isFile()) { System.out.println("File: " + file.getName()); } } }</code>
Note:
The above is the detailed content of How to List All Filenames in a Java Directory?. For more information, please follow other related articles on the PHP Chinese website!