Java Recursive directory listings
- October 29th, 2009
- Posted in Development . Java
- Write comment
A simple and yet troublesome for some, part of Java coding, is the ability to move recursively through directory structures. When processing directories full of files this is most definitely needed. We can accomplish this with a simple logic jump. We need to create a method to process all the files of a given directory, that when a directory is encountered calls itself passing in the new File that contains the directory.
The Test Directories
So to start out we need a directory to test with. I created a C:/test directory with example1.txt and example2.txt files. I then created the directory C:/test/exampledir and within that directory example3.txt and example4.txt.
The Main Method
package recurse;public static void main(String[] args){public class Main {// Pass in the test directoryExample.recurse("C:/test");}}
From the code above you can see we are calling a class named Example and within that class a method named recurse, and into that method we are passing a the directory path as a String.
The Example Class
package recurse;
import java.io.File;
public class Example {
public static void recurse(String dir){
}
At the start you can see that I have made this method have a return type of void this is because we are not going to pass back any information. this can easily be changed but is out of the scope of this tutorial. So lets set about collecting and dealing with the Files, firstly we need to store the directory we have just passed in.
// create file object for directory
File directory = new File(dir);
We do this in a File object, we now need to list all the files within that directory, but we need somewhere to store them. Creating a String array here is probably the simplest answer.
// get list of files from directory into a String array
String[] files = directory.list()
now with all the files in that directory contained within the String array we are set. I would like to pause here and make a very important point that may have already occurred to you, but is easily missed. The File object is not only capable of containing files but directories too, this point is very important to remember as we continue.
The Loop
We are at the pivotal point. Now we must loop through the files contained within the String array and determine what we need to do with each file.
// loop through files
for(int i = 0; i < files.length; i++){
// place current file string within a file object
procFile = new File(directory,files[i]);
// use file object to check if current file is a directory
if(procFile.isDirectory()){
// if file is a directory recurse
recurse(procFile.getPath());
}else{
// print out file name
System.out.println("The current file: " + procFile.getPath());
}
}
We can now assign the latest entry to be called by the loop to the procFile File object. then we test to see if procFile is a directory, if it is we call our self passing in procFile which will become the directory at the start of the method, allowing us to drop down and eventually crawl back up the tree directory structure. If however the procFile was not a directory its path is printed out.
Hope this helps let me know if you have any questions.
No comments yet.