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:
- Take as a single command line argument the name of the folder you want to explore as a tree (e.g., the String
warmupin our example above). - 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); - Create a tree (using your
MyTreeNodeclass) that contains all of the files and folders in the directory given as a command line argument. Theitemof eachMyTreeNodewill be theStringname of a folder or file. - Print the contents of the directory saved in the tree created in Step 2 using the
print()method of yourMyTreeNodeclass.
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:
- Takes two parameters: (1) a
String filenamerepresenting the name of the folder or file that we need to add to our tree, and (2) aMyTreeNode<String> parentthat will be theparentof our new node. - Creates a new
MyTreeNode<String>to store the givenfilenameas a child of the passed inparent(or a new root node if theparentisnull) - Finds all of the subfolders and files within the directory represented by
filename - Recursively calls the
createNodemethod 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 3 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:
- The
File(filename)constructor takes as a single parameter the name of the folder (or file) and returns aFileobject representing the folder (or file). - The
listFiles()method returns aFile[]array containing all of the subfolders and files within its own folder.
- If a given
Fileis a file and not a folder, it does not contain any subfolders or files, so the array returned by thelistFiles()method isnull.
- The
getPath()method returns a String that contains the relative path from our current directory and theFile.
- The path should be the
itemstored in ourMyTreeNodes.
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.