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
You should implement your program in a class called ExploreDirectory
(in a .java
file of the same name). The program should:
warmup
in our example above).System.exit(-1);
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.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.
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:
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.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
)filename
createNode
method to create new children nodes of the new node created in Step 2 for each file that was found in Step 3This 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:
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).listFiles()
method returns a File[]
array containing all of the subfolders and files within its own folder.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
.getPath()
method returns a String that contains the relative path from our current directory and the File
.item
stored in our MyTreeNode
s.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!).
Don’t forget to frequently save your progress periodically to GitHub using the add
, commit
, and push
commands with git.