Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io . File; public class DisplayDirectoryStructure { public static void main ( String [ ] args ) { / / Get user input for

import java.io.File;
public class DisplayDirectoryStructure {
public static void main(String[] args){
// Get user input for the directory path
System.out.print("Enter the path: ");
Scanner scanner = new Scanner(System.in);
String userPath = scanner.nextLine();
scanner.close();
// Display directory structure
displayDirectoryStructure(userPath);
}
public static void displayDirectoryStructure(String path){
int numFiles =0;
int numDirectories =0;
long totalSize =0;
displayDir(path,"", numFiles, numDirectories, totalSize);
// Display summary
System.out.println("
Number of Files: "+ numFiles);
System.out.println("Number of Directories: "+ numDirectories);
System.out.println("Total Size: "+ totalSize +" bytes");
}
private static void displayDir(String path, String prefix, int numFiles, int numDirectories, long totalSize){
File directory = new File(path);
File[] entries = directory.listFiles();
if (entries != null){
for (File entry : entries){
if (entry.isFile()){
System.out.println(prefix +"+--"+ entry.getName()+""+ entry.length()+" bytes");
totalSize += entry.length();
numFiles++;
} else if (entry.isDirectory()){
System.out.println(prefix +"+--"+ entry.getName());
numDirectories++;
displayDir(entry.getPath(), prefix +"|", numFiles, numDirectories, totalSize);
}
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

=+3. What level of candor are decision makers willing to receive?

Answered: 1 week ago