How do I recursively display all files and directories in a directory?
Author: Deron Eriksson
Description: This Java tutorial describes how to recursively traverse all the files and subdirectories in a directory.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


In another tutorial, we saw how we could get an array of the File objects (which can be files or directories) in a directory File object via a call to its listFiles() method. We also saw how we could determine if a File object is a directory or a file by calling its isDirectory() method. However, sometimes we want to access everything below a certain directory including all subdirectories and their files, not just the immediate level of files. We can do with with recursion.

The RecursiveFileDisplay class demonstrates how to do this. We obtain a File object representing the current directory and pass that File object to displayDirectoryContents(). The displayDirectoryContents() gets the array of File objects that the directory contains via the call to listFiles(). It loops over this array using a for loop. If the File object is a file, it displays "file:" followed by the file canonical path. If it is a directory, it displays "directory:" followed by the directory canonical path. However, notice what else it does if the File object is a directory. It also calls the displayDirectoryContents() method with this File object. So, the displayDirectoryContents() method calls displayDirectoryContents() with the current File object, which allows it to recursively display all directories and files within the 'top' directory (in our case, the 'current' directory).

RecursiveFileDisplay.java

package test;

import java.io.File;
import java.io.IOException;

public class RecursiveFileDisplay {

	public static void main(String[] args) {
		File currentDir = new File("."); // current directory
		displayDirectoryContents(currentDir);
	}

	public static void displayDirectoryContents(File dir) {
		try {
			File[] files = dir.listFiles();
			for (File file : files) {
				if (file.isDirectory()) {
					System.out.println("directory:" + file.getCanonicalPath());
					displayDirectoryContents(file);
				} else {
					System.out.println("     file:" + file.getCanonicalPath());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

We'll execute RecursiveFileDisplay on the following project:

'testing' project

The console output from the execution of RecursiveFileDisplay is shown below. As you can see, it matches the expected results based on the structure of the project.

     file:C:\projects\workspace\testing\.classpath
     file:C:\projects\workspace\testing\.project
directory:C:\projects\workspace\testing\bin
directory:C:\projects\workspace\testing\bin\test
     file:C:\projects\workspace\testing\bin\test\RecursiveFileDisplay.class
directory:C:\projects\workspace\testing\f1
directory:C:\projects\workspace\testing\f1\f2
directory:C:\projects\workspace\testing\f1\f2\f3
     file:C:\projects\workspace\testing\f1\f2\f3\file5.txt
     file:C:\projects\workspace\testing\file1.txt
     file:C:\projects\workspace\testing\file2.txt
directory:C:\projects\workspace\testing\folder
     file:C:\projects\workspace\testing\folder\file3.txt
     file:C:\projects\workspace\testing\folder\file4.txt
directory:C:\projects\workspace\testing\lib
     file:C:\projects\workspace\testing\lib\log4j-1.2.14.jar
directory:C:\projects\workspace\testing\src
directory:C:\projects\workspace\testing\src\test
     file:C:\projects\workspace\testing\src\test\RecursiveFileDisplay.java


Related Tutorials: