Tuesday, 25th October 2011
Follow WikiJava on twitter now. @Wikijava

Printing the directory tree

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

This example is a full program that reads the directory structure and prints it formatted on the standard output.

I've actually created this program for showing the directory structures in the tutorials on WikiJava :D


Contents

the article

The program expects to be executed with one parameter, representing the starting directory from which build the directory tree. If no parameters are passed the current directory is used.

The core of the program is the method;

private static StringBuffer exploreDirectory(File directory, int level)

that recursively explores all the subdirectories and prints them along with the files contained in each directory.

fileTree.java

Image:250px-Subversion.png
You can download the complete code of this article from the Subversion repository at this link

Using the username:readonly and password: readonly

See the using the SVN repository instructions page for more help about this.
/**
 * 
 */
package org.wikijava.utils.fileTree;
 
import java.io.File;
import java.util.Arrays;
import java.util.List;
 
/**
 * generates a visual ascii representation of a directory tree
 * 
 * @author Giulio
 */
public class FileTree {
 
    private static final String USAGE = "usage \n fileTree <directory name>"; //$NON-NLS-1$
 
    private static final String NEWLINE = "\n"; //$NON-NLS-1$
 
    private static final String FILE_GRAPHIC = "- "; //$NON-NLS-1$
 
    private static final String DIRECTORY_GRAPHIC = "+- "; //$NON-NLS-1$
 
    private static final String CURRENT_PATH = "."; //$NON-NLS-1$
 
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
	// parameters check
	if (args.length > 1) {
	    System.err.println(USAGE);
	    return;
	}
 
	// assign parameters
	File directory = null;
	if (args.length == 1) {
	    String baseDir = args[0];
	    directory = new File(baseDir);
	} else {
	    directory = new File(CURRENT_PATH);
	}
 
	if (!directory.isDirectory()){
	    System.err.println(USAGE);
	    return;
	}
 
	StringBuffer stringBuffer = exploreDirectory(directory, 0);
 
	System.out.println(stringBuffer);
    }
 
    /**
     * 
     * explores the directory tree
     * 
     * @param directory
     *                the directory to start with
     * @param level
     *                the level of indentation
     * @return a String ascii representation of the directory Structure
     */
    private static StringBuffer exploreDirectory(File directory, int level) {
	List<File> files = Arrays.asList(directory.listFiles());
	StringBuffer result = new StringBuffer();
 
	// creates the spacing
	StringBuffer spaces = new StringBuffer();
	for (int i = 0; i < level; i++) {
	    spaces.append(" "); //$NON-NLS-1$
	}
 
	// explores the directories
	for (File cur : files) {
	    if (cur.isDirectory()) {
		result.append(spaces + DIRECTORY_GRAPHIC + cur.getName()
			+ NEWLINE);
		result.append(exploreDirectory(cur, level
			+ DIRECTORY_GRAPHIC.length() - 2));
	    }
	}
 
	// outputs the files
	for (File cur : files) {
	    if (cur.isFile()) {
		result.append(spaces + FILE_GRAPHIC + cur.getName() + NEWLINE);
	    }
	}
	return result;
    }
}

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.