// CSE 143, summer 2012 // Prints contents of directories using indentation to indicate // nesting. import java.io.*; import java.util.*; public class Crawler { 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 { crawl(f); } } // Prints the given file or directory and any files or directories // inside it, starting with 0 indentation. public static void crawl(File f) { crawl (f, ""); } // Prints the given file or directory and any files or directories // inside it at the given level of indentation. // This is an example of a recursive helper method. // Pre: f != null && f.exists() private static void crawl(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { File [] files = f.listFiles(); for (int i = 0; i < files.length; i++) { crawl (files[i], indent + " "); } } } }