How to traverse a directory structure in Java
Published: January 12, 2010 , Updated: January 6, 2010 , Author: mkyong
In this example, the program will traverse the given directory and print out all the directories and files absolute path and name one by one.
Example
package com.mkyong.io; import java.io.File; public class DisplayDirectoryAndFile{ public static void main (String args[]) { displayIt(new File("C:\\Downloads")); } public static void displayIt(File node){ System.out.println(node.getAbsoluteFile()); if(node.isDirectory()){ String[] subNote = node.list(); for(String filename : subNote){ displayIt(new File(node, filename)); } } } }
Output
C:\Downloads C:\Downloads\100 Java Tips.pdf C:\Downloads\1590599799.rar C:\Downloads\2009 C:\Downloads\573440.flv C:\Downloads\575492.flv C:\Downloads\avira_antivir_personal_en.exe C:\Downloads\backup-mkyong.com-12-24-2009.tar.gz ......
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
The following example shows printing directory hierarchy as a TREE just like DOS tree command.
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_Core_General_FileMap
Good luck