Clean directories with recursion – Code

Clean directories with recursion is to uses Java File class and methods to remove files in a file system. What is recursion? Recursion is a technique that a function or an algorithm calls itself. It is powerful tool to solve math, and data structures problems. It solves sub and repetitive problems. when the condition is met, the rest of each repetition is processed from the last call to the first. Here we use it to in file system.

In Java, File class provides the methods to access file system. For example, you can use isDirectory(), listFiles() and getAbsolutePath() to search. The you can also add or delete files and directories.

LinkedIn Interview Question:
Write a function that cleans out files older than a user specified number of days, and removes empty directories recursively.

Java Code:

file is deleted.
file is deleted.
file is deleted.
file is deleted.
Folder Name :folder\oldfiles\subfolder is deleted.

O notation:
Time complexity is O(n), n is the number of files/directories.
space complexity is O(m), m is the depth of the recursion.

If you have any questions or want to put comments, please post at youtube. I will answer you!
The code displayed here is the simplified version after the enhancement in the 2nd edition of Java coding interview pocket book. Please click the top download button to download the original version.

Clean directories with recursion tutorial
The complete list of coding interview questions

Comments are closed