// CSE 143, winter 2012 // Prints contents of directories using indentation to indicate // nesting. Example: // lol cats // cs // hex.jpeg // recursive.jpeg // sangwich.jpeg 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 { print(f); } } // Prints the given file or directory and any files or directories // inside it, starting with 0 indentation. public static void print(File f) { print(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 print(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { File[] contents = f.listFiles(); for (int i = 0; i < contents.length; i++) { print(contents[i], indent + " "); } } // base case: just stop! } }