
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 < sub_level; i++) System.out.print("\t"); if(my_arr[my_index].isFile()) System.out.println(my_arr[my_index].getName()); else if(my_arr[my_index].isDirectory()){ System.out.println("[" + my_arr[my_index].getName() + "]"); print_recursively(my_arr[my_index].listFiles(), 0, sub_level + 1); } print_recursively(my_arr,++my_index, sub_level); } public static void main(String[] args){ String path_main = "path to folder that contains files"; File main_dir = new File(path_main); if(main_dir.exists() && main_dir.isDirectory()){ File my_arr[] = main_dir.listFiles(); System.out.println("Files listed from the main directory are : " + main_dir); print_recursively(my_arr,0,0); } } }
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.
- Related Articles
- C Program to list all files and sub-directories in a directory
- Java program to List all files in a directory recursively
- How to list all files in a directory using Java?
- How to list all files (only) from a directory using Java?
- Java program to delete all the files in a directory recursively (only files)
- How to get all the files, sub files and their size inside a directory in C#?
- Recursively List All Files in a Directory Including Symlinks
- Java program to merge contents of all the files in a directory
- Golang program to get all files present in a directory
- How to list out the hidden files in a Directory using Java program?
- PHP: Unlink All Files Within A Directory, and then Deleting That Directory
- How to list the hidden files in a directory in Java?
- How do I list all files of a directory in Python?
- How to list down all the files available in a directory using C#?
- How to read data from all files in a directory using Java?

Advertisements