// 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.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 name of the given file. If the file is a directory, // recursively prints its content. Indents each subdirectory. // pre: f.exists() public static void print(File f) { print(f, 0); } // Prints the name of the given file. If the file is a directory, // recursively prints its content. Prints at the given level of // indentation. // pre: f.exists() private static void print(File f, int indentLevel) { for (int i = 0; i < indentLevel; i++) { System.out.print(" "); } System.out.println(f.getName()); if (f.isDirectory()) { for (File subF : f.listFiles()) { print(subF, indentLevel + 1); } } } }