Subscribe to
Posts [Atom]


Java Class To View A Directory

Wednesday, April 16, 2008

This static class will let you view the contents of a directory. The file you will pass the listPath method will look like "/File/File" for unix, linux, and mac or "c:\\" for those Windows losers.

public class Dir {
static int indentLevel = -1;

public static void listPath(File path) {
File files[];
indentLevel++;

files = path.listFiles();

Arrays.sort(files);
for (int i = 0, n = files.length; i < n; i++) {
for (int indent = 0; indent < indentLevel; indent++) {
System.out.print(" ");
}
System.out.println(files[i].toString());
if (files[i].isDirectory()) {

listPath(files[i]);
}
}
indentLevel--;
}
}

Labels:


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home