/* * Kyle Pierce * 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 (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); } } // post: prints the name of the file // if the file is a directory, print its children with // indentation to show nesting public static void print(File f) { print(f, 0); } // post: print the name of the file at the given indentation level // if the file is a directory, print its children at a // higher indentation indent private static void print(File f, int indent) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.println(f.getName()); if (f.isDirectory()) { for (File subF : f.listFiles()) { print(subF, indent + 1); } } } }