// Helene Martin, CSE 143 // Prompts the user for a file or directory name and shows // a listing of all files and directories that can be reached from it. 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, // prints its content. // pre: f.exists() public static void print(File f) { print(f, 0); } // Prints the name of the given file at the specified level of indentation. // If the file is a directory, prints its content. // pre: f.exists() private static void print(File f, int level) { // could also pass indentation as a String for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(f.getName()); if (f.isDirectory()) { File[] contents = f.listFiles(); for (int i = 0; i < contents.length; i++) { File current = contents[i]; print(current, level + 1); } } } }