-
-
Save durgeshgowdac/edb58670bd65ef45742e9ee522d5ea99 to your computer and use it in GitHub Desktop.
Find the deepest file in a system file tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Scanner; | |
class Main { | |
static int maxLevel = 0; | |
static String deepestFileName = null; | |
static int currentLevel = 0; | |
static String deepestPath = null; | |
static Scanner sc = new Scanner(System.in); | |
public static void main(String[] args) { | |
System.out.print("Enter the absolute path of the folder: "); | |
File file = new File(sc.next()); | |
goRecursive(file); | |
System.out.printf("\nFile Name: %s\nAbsolute Path: %s\nLevel from input directory: %d", deepestFileName, | |
deepestPath, maxLevel); | |
} | |
public static void goRecursive(File dir) { | |
if (currentLevel > maxLevel) { | |
deepestFileName = dir.getName(); | |
deepestPath = dir.getAbsolutePath(); | |
} | |
if (dir.isDirectory() && dir.listFiles().length != 0) { | |
++currentLevel; | |
for (File subdir : dir.listFiles()) { | |
goRecursive(subdir); | |
} | |
maxLevel = Math.max(maxLevel, currentLevel); | |
--currentLevel; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment