List file structure within folder or hard drive

This code example lists the files and folders and also the content in all subfolders.
With recursive calls the file/directory tree can be listed with a relatively small amount of code.
The purpose of the getTabs method and the variable tabCounter is only to make the output more viewable with indenting for each level.

What we do is to create a new File object with a path to the folder from where we want to begin to list the content.
Then we check that the File object really exists and that it points to a folder (not a physical file). If it does, we exit the program.
The we use the Arrays class to convert the array to a Collection object (a List) and use foreach method to loop on that list, passing an anonymous function to it.
For each item in the list we print out the name, and depending on what level in the structure the item is in we add tabs before the name to make it more viewable.
Down in the main-method we create an instance of the FileStructure class and provide the directory as parameter to the listFilesAndFolders method.

Search for more Java info on this site here:
Custom Search

Please type any questions here.