// Hunter Schafer, CSE 143 // This program prints out the structure of a given directory or file import java.io.File; import java.util.Scanner; public class DirectoryCrawler { 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 out the name of the given file. // If the given file is a directory, prints the information about its children // The output is indented like any file browser. // pre: file.exists() public static void print(File file) { print(file, 0); } // Prints out the name of the given file with indent levels of indentation (4 spaces each) // If the given file is a directory, prints the information about its children. // pre: f.exists() private static void print(File file, int indent) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.println(file.getName()); if (file.isDirectory()) { // directory File[] subFiles = file.listFiles(); // You could use a for-each loop here since we don't really care about i! for (int i = 0; i < subFiles.length; i++) { print(subFiles[i], indent + 1); } } } }