How to search directory structure recursively – Java list files example

Today’s tutorial is about listing all the files in the given directory and gathering statistics like number of directories and files. Our code moves recursively down the directory structure. We have written listDirectory method which displays all the nodes in the given directory. File.list() method is used to list the nodes. For each node we check if it is a file or a directory by calling File.isDirectory() method on it. For directories the listDirectory method is called to process it the same way – recursive call is used. During directory tree search statistics is being gathered which is displayed at the end of the code.

Java list files and directories – recursive search code

package com.itcuties.java.files;

import java.io.File;

/**
 * This code lists directory contents
 * 
 * @author itcuties
 */
public class ListFilesInDirectory {
	
	// Files and directories statistics
	private static int allElementsCount = 0;
	private static int directoriesCount = 0;
	
	public static void main(String[] args) {
		// Scanned directory
		String directoryPath = "d:/tmp";
		
		// Display directory name
		System.out.println(directoryPath);
		// Go recursively down
		listDirectory(directoryPath);
		
		System.out.println("\n-----------------------------------------------");
		System.out.println("Directory stats");
		System.out.println("-----------------------------------------------");
		System.out.println("Directories: " + directoriesCount);
		System.out.println("Files: " + (allElementsCount - directoriesCount));
	}
	
	/**
	 * Visit each directory recursively, count and display its contents
	 * @param directoryPath
	 */
	public static void listDirectory(String directoryPath) {
		// List all the directory contents
		for (String contents: new File(directoryPath).list()) {
			
			allElementsCount++;
			
			// Directory path for files and directories
			contents = directoryPath+"/"+contents;
			
			// Display full path names
			System.out.println(contents);
			
			// For directories go recursively down
			if (new File(contents).isDirectory()) {
				
				directoriesCount++;
				
				// Go down to next directory
				listDirectory(contents);
			}
		}
	}	
}

Output:

d:/tmp
d:/tmp/java-read-url
d:/tmp/java-read-url/pom.xml
d:/tmp/java-read-url/src
d:/tmp/java-read-url/src/main
d:/tmp/java-read-url/src/main/java
d:/tmp/java-read-url/src/main/java/com
d:/tmp/java-read-url/src/main/java/com/itcuties
d:/tmp/java-read-url/src/main/java/com/itcuties/java
d:/tmp/java-read-url/src/main/java/com/itcuties/java/ReadFromURLExample.java
d:/tmp/java-read-url/src/main/resources
d:/tmp/java-read-url/src/test
d:/tmp/java-read-url/src/test/java
d:/tmp/java-read-url/src/test/resources
d:/tmp/java-read-url.zip
d:/tmp/server.log
d:/tmp/server.log.zip

-----------------------------------------------
Directory stats
-----------------------------------------------
Directories: 11
Files: 5

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>