Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Java program to List all files in a directory and nested sub-directory - Recursive approach
To list all files in a directory and nested sub-directory, the Java program is as follows −
Example
import java.io.File;
public class Demo{
static void print_recursively(File[] my_arr,int my_index,int sub_level){
if(my_index == my_arr.length)
return;
for (int i = 0; i Output
Inside the directory path, all the files of all formats from the path will be listed
A class named Demo contains a function named ‘print_recursively’ that takes the array and the
index value, and nested levels as parameters, and iterates through the files and returns the list of all
files in that specific directory.
In the main function, the path to the folder with all the files is defined and a new file path is also
created. The ‘listFiles’ function is used to list all the files in that specific folder. Next, the function is
called by passing these parameters. Result is displayed on the console.
Advertisements
