Traverse Directories Find File Java

Traverse Directories Find File Java

If you want to traverse all files inside directory including its sub directory files please refer below sample program:

  • Sample java class:
package com.javahonk;

import java.io.File;

public class TraverseDirectoryFindFile {

	public static void main(String[] args) {
		
		File file = new File("C:\\JavaHonk\\zip\\SpringMVCRESTFulService");
		displayFiles(file);
	}

	private static void displayFiles(File file) {
		
		 System.out.println(file.getAbsoluteFile());
		 
		if (file.isDirectory()) {
			String[] listOfFiles = file.list();
			for (String filename : listOfFiles) {
				displayFiles(new File(file, filename));
			}
		}
	}
}
  • Output:

Traverse Directories Find File Java

For more information on traversing directories please refer oracle documentation here

Leave a Reply

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