import java.io.*; import java.util.*; // This program prints out the structure of a given directory or file public class DirectoryCrawler { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("directory or file name? "); String name = console.nextLine(); File f = new File(name); if (!f.exists()) { System.out.println("That file or directory does not exist"); } else { print(f); } } // pre: f.exists() // post: prints the name of the file // if the file is a directory, prints a complete listing of all // the files under this directory public static void print(File f) { print(f, 0); } // pre: f.exists() // post: prints the name of the file with the given level of indentation // if the file is a directory, prints a complete listing of all // the files under this directory with more indentation private static void print(File f, int numIndents) { for (int i = 0; i < numIndents; i++) { System.out.print(" "); } System.out.println(f.getName()); if (f.isDirectory()) { // print out everything under this directorys File[] subFiles = f.listFiles(); for (int i = 0; i < subFiles.length; i++) { File subFile = subFiles[i]; print(subFile, numIndents + 1); } } } }