Return to Snippet

Revision: 37276
at December 7, 2010 20:09 by hariharank12


Initial Code
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 */

/**
 * @author hariharan_kumar
 *
 */
public class Dfs {

	List<String> directoryLists;
	/**
	 * 
	 */
	public Dfs() {
		
		directoryLists = new ArrayList<String>();
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Dfs df = new Dfs();
		df.iterate("G:/jbossTest");
		df.displayDirList();
		
	}

	public void iterate(String baseDir)
	{
		File folder_and_files = new File(baseDir);
		File[] file_array = folder_and_files.listFiles();
		
		for(int i = 0;i < file_array.length;i++)
		{
			if(file_array[i].isDirectory())
			{
				//System.out.println("Directory  is : " + file_array[i].getName());
				directoryLists.add(file_array[i].getAbsolutePath());
				iterate(file_array[i].getAbsolutePath());
			}
			else if (file_array[i].isFile())
			{
				//System.out.println("File is : " + file_array[i].getAbsolutePath());
				
			}
			else if(file_array.length == i)
			{
				return;
			}
				
		}		
	}
	
	public void displayDirList()
	{
		Iterator<String> i = directoryLists.iterator();
		while(i.hasNext())
		{
			System.out.println(i.next());
		}
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
To list sub directories recursively in a given directory

Initial Tags

                                

Initial Language
Java