List all files in a Directory in Java
The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some of the files. Those files are also a part of the main directory. Therefore, we must mention them.
directories without Sub-directories
It denotes a folder's file-only status. The steps which are given below will describe how to print the files
1. create an object for the directory in step one.
2. The second step is to get the directory's arrays of files.
3. Print each file's name (including extension) one at a time in a recursive fashion.
Implementation
The code below outputs the file names found in the Documents folder. The files found in the Documents folder are displayed in the screenshot below.

DisplayFile.java
//this program is for a list of all the documents in the directory in java
//import section
import java.io.File;
//The class DisplayFile is created
public class DisplayFile
{
public void printNames(File[] array, int i, int lvl)
{
//initializing the base condition
// if the condition i==array.length then there are
// no more files.The loop will terminate.
if(i == array.length)
{
return;
}
// checking whether it is f file or not
if(array[i].isFile())
{
System.out.println(array[i].getName());
}
// The files in the directory are printed according to the recursion process
// the value i+1 means checking the next file in the directory
printFileNames(array, i + 1, lvl);
}
// Main section
public static void main(String[] argvs)
{
// the path of the directory
String path = "/Users/mprashanthrao/Documents/My documents";
//A file object fobject was created
File fObject= new File(path);
// an object obj i created for the class DisplayFile
DisplayFile obj = new DisplayFile();
if(fObject.exists() && fObjcte.isDirectory())
{
// the files in the directory are printed by using the fobject
File array[] = fObject.listFiles();
// display the required statements
System.out.println("-----------------------------------------------------------------------");
System.out.println("The files in the directory : " + fObject);
System.out.println("-----------------------------------------------------------------------");
// the method for calling
obj.printNames(array, 0, 0);
}
}
}
Output:

subdirectory-containing directory
The phrase indicates that a folder has both files and subfolders. The steps will show how to display the directory's files and those of its subdirectories.
- Create a File Object for the directory in step one.
- The second step is to get the directory's array containing files and subdirectories.
- It will show the file name, and the subsequent array[j] element will be reached recursively in step 3 if array[j] is a file.
- Repeat step 2 in step 4 if array[j] is indeed a directory and shows the directory name.
DisplayFileEx1.java
// This program is for a list of all documents in the given document
//import section
import java.io.File;
public class DisplayFileEx1
{
public void printFiles(File[] array, int i, int level)
{
//initializing the base condition
// if the condition i==array.length then there are
// no more files.The loop will terminate.
if(i == array.length)
{
return;
}
//files are provided by indentation
for (int j = 0; j < level; j++)
{
System.out.print("\t");
}
// the condition will checks whether it is the file or not
if(array[i].isFile())
{
System.out.println(array[i].getName());
}
//reading the files in subdirectories
else if(array[i].isDirectory())
{
System.out.println("[" + array[i].getName() + "]"); z
//the files read until the condition
printFileNames(array[i].listFiles(), 0, level + 1);
}
//The files in the directory are printed according to the recursion process
// the value i+1 means checking the next file in the directory
printFileNames(array, i + 1, level);
}
// Main section
public static void main(String[] argvs)
{
// Providing the full path for the directory
String path = "";
// creating a file object
File fObject = new File(path);
// creating an object of the class DisplayFileExample1
DisplayFileEx1 obj = new DisplayFileEx1();
if(fObject.exists() && fObject.isDirectory())
{
// array contains the files belonging to that directory pointed by fObject
File array[] = fObject.listFiles();
// display the required statements
System.out.println("-----------------------------------------------------------------------");
System.out.println("The files in the directory : " + fObject);
System.out.println("-----------------------------------------------------------------------");
// the method for calling
obj.printFiles(array, 0, 0);
}
}
}
Output:
