Wednesday, May 26, 2010

How to traverse a directory structure in Java


the program will traverse the given directory and print out all the directories and files absolute path and name one by one.

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));
}
}
 
}
}

No comments:

Post a Comment