// CSE 143, Autumn 2013 // Prints contents of directories using indentation to indicate // nesting. Example: // lol cats // cs // hex.jpeg // recursive.jpeg // sandwich.jpeg 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 given file or directory and any files or directories // inside it, starting with 0 indentation. public static void print(File f) { print(f, ""); } private static void print(File f, String spaces) { System.out.println(spaces + f.getName()); if(f.isDirectory()) { // get all of the files in this file File [] contents = f.listFiles(); for(int i = 0; i < contents.length; i++) { print(contents[i], spaces + " "); } // if they are directories get all their files } } }