Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Add the following things to the java code import java.io.File; import java.util.Scanner; public class RecursiveFileDirectoryDisplay { public static void main(String[] args) { Scanner scnr =
Add the following things to the java code
import java.io.File;
import java.util.Scanner;
public class RecursiveFileDirectoryDisplay {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);//for input
System.out.print("Please Enter Root Directory: ");//prompt user
String rootDirectory = scnr.nextLine();//set rootDirectory to user input
System.out.println("List of all Directory & files under " + rootDirectory);
System.out.println("------------------------------------");
displayDirectoryContents(rootDirectory);// call method to recurse and print
}
public static void displayDirectoryContents(String rootDirectory) {
File root = new File(rootDirectory);
if (root.exists()) { //if root directory exists
File[] list = root.listFiles(); //create array of all files in root directory
if (list != null) {//if list is not null
for (File f : list) {
if (f.isDirectory()) {//if it is a directory
System.out.println("Directory: " + f.getAbsoluteFile());// print
displayDirectoryContents(f.getAbsolutePath());//send back in
} else {//else it is a file
System.out.println("File : " + f.getAbsoluteFile());// print the file name
}
}
}
else {//
System.out.println("Input root directory is not exists!");// print error message
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started