free web hosting | website hosting | Web Hosting | Free Website Submission | shopping cart | Promoter Online | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
Recursive Directory Search (Depth First Search) in Java

Recursive Directory Search (Depth First Search) in Java

To do a thorough search of all files in a directory, the search function must be called recursively. Namely, if a directory file is found, all the files under that directory must be processed.


  if(files[i].isDirectory())
    {
	depthFirstSearch(files[i]);
    }

Here, depthFirstSearch(File) is the search function that processes a file or directory. To list up all the files under a directory, call listFiles() function of the File class.


  int i;
  File [] files;	
  for(files=dir.listFiles(), i=0;files!=null && i<files.length; i++)
  { ...
  }

Suppose, say, you want to list up every file under a directory that contains non BASIC_LATIN character in its file name.


  if(Character.UnicodeBlock.of(ch)!=Character.UnicodeBlock.BASIC_LATIN)
     {
	found=true;
     }

Click here for a sample Java source code which does a depth first search of a directory. This program searches every file under a directory which is specified as an argument (current directory if no directory is specified), and prints out the path names of the files that have any character that contain non BASIC_LATIN character.


Return to Home
Erica Asai
Last Modified: Fri Sep 03 11:32:13 2004