Exploring the File System

Application Overview

The file system on our computers organizes all of our files into a series of folders (also called directories). Earlier in the semester, we practiced navigating through our file system using the terminal commands cd and cat. In this final part of the lab, we will see how our file system can be modeled as a tree data structure and recursively printed to see all the contents of a folder (including all of its subfolders).

For example, the “warmups” folder in your GitHub repository contains two subfolders called “A” and “B” and no files. The “A” folder contains no subfolders and two files “1.jpg” and “2.jpg”. The “B” folder contains two subfolders “1” and “2” and one file “cat1.jpeg”. The “1” folder contains no subfolders and two files “cat2.jpg” and “cat3.jpg”. Finally, the “2” folder contains no subfolders and three files “cat4.jpg”, “cat5.jpg”, and “cat6.jpg”.

We could represent this organization with a tree that recursively displays using the print method developed Part 3 of the lab as:

warmup
	warmup/A
		warmup/A/2.jpg
		warmup/A/1.jpg
	warmup/B
		warmup/B/cat1.jpeg
		warmup/B/1
			warmup/B/1/cat2.jpg
			warmup/B/1/cat3.jpg
		warmup/B/2
			warmup/B/2/cat6.jpg
			warmup/B/2/cat4.jpg
			warmup/B/2/cat5.jpg

Application Instructions

You should implement your program in a class called ExploreDirectory (in a .java file of the same name). The program should:

  1. Take as a single command line argument the name of the folder you want to explore as a tree (e.g., the String warmup in our example above).
  2. If there wasn’t exactly one command line argument passed in, your program should print a reminder of how to run the program and quit using System.exit(-1);
  3. Create a tree (using your MyTreeNode class) that contains all of the files and folders in the directory given as a command line argument. The item of each MyTreeNode will be the String name of a folder or file.
  4. Print the contents of the directory saved in the tree created in Step 2 using the print() method of your MyTreeNode class.

The steps above should be implemented in the public static void main(String[] args) method of your ExploreDirectory class.

Recursing through the File System

To build up the tree for a given folder, we will need to use recursion to iterate through all of the subfolders and files. In addition to your main method described above, you should implement a recursive method createNode that:

  1. Takes two parameters: (1) a String filename representing the name of the folder or file that we need to add to our tree, and (2) a MyTreeNode<String> parent that will be the parent of our new node.
  2. Creates a new MyTreeNode<String> to store the given filename as a child of the passed in parent (or a new root node if the parent is null)
  3. Finds all of the subfolders and files within the directory represented by filename
  4. Recursively calls the createNode method to create new children nodes of the new node created in Step 2 for each file that was found in Step 3

This createNode method can be called as Step 2 of your main method above.

ReadMe

To help us get the names of all the files and subfolders of a given folder name, we can use Java’s built-in java.io.File class. Some useful methods of the File class include:

  1. The File(filename) constructor takes as a single parameter the name of the folder (or file) and returns a File object representing the folder (or file).
  2. The listFiles() method returns a File[] array containing all of the subfolders and files within its own folder.
  • If a given File is a file and not a folder, it does not contain any subfolders or files, so the array returned by the listFiles() method is null.
  1. The getPath() method returns a String that contains the relative path from our current directory and the File.
  • The path should be the item stored in our MyTreeNodes.

Testing Our Program

To test our program, we can use the String warmup as the command line argument, which should print the tree whose output matches the example output above (the order of files might be slightly different since the File.listFiles() method might return them in a different order, but every folder/file should all have the same indentation).

You can also try running your program with different command line arguments to explore other parts of the file system! For example, the String . (a single period) will show you all of the contents of your current folder (your GitHub repository), and the String ~ (the tilde character) should show you the contents of your entire user home directory (which might take a little while to display!).

Committing Your Progress

Don’t forget to frequently save your progress periodically to GitHub using the add, commit, and push commands with git.