// This program prompts the user for a file or directory name and shows // a listing of all files and directories that can be reached from it // (including subdirectories). import java.io.*; import java.util.*; public class Crawler { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter the full directory or file name " + "(ex: /Users/moorec22/Documents/...)"); 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, 0); } } // pre : f.exists() // post: prints a directory listing for the given file using the given // level of indentation. Prints just the file name for a file. // For a directory, prints the name and a complete listing of all // files/directories under this directory, using indentation to // indicate the level. public static void print(File f, int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(f.getName()); if (f.isDirectory()) { for (File subF : f.listFiles()) { // can't do level++, or we are reassigning level // in this scope as well print(subF, level + 1); } } } // alternate version of the above method, showing how to use a String // to build up indentation, instead of using an int // would be called with print(f, "") public static void print(File f, String indentation) { System.out.println(indentation + f.getName()); if (f.isDirectory()) { for (File subF : f.listFiles()) { // the equivalent index++ bug for this solution would be // reassigning indention = " " + indentation; print(subF, " " + indentation); } } } }