Question
Write a program that uses a JavaFX GUI that allows a user to enter a directory (folder) and then displays the number of the files
Write a program that uses a JavaFX GUI that allows a user to enter a directory (folder) and then displays the number of the files in the directory. The number of files in the directory should be determined recursively.
Use the code below to make into GUI.
import java.io.File; import java.util.Scanner;
public class DirectorySize { public static void main(String[] args) { // Prompt the user to enter a directory or a file System.out.print("Enter a directory or a file: "); Scanner input = new Scanner(System.in); String directory = input.nextLine(); // Display the size System.out.println(getSize(new File(directory)) + " file(s)"); }
public static long getSize(File file) { long size = 0; // Store the total size of all files
if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; files != null && i < files.length; i++) { size += getSize(files[i]); // Recursive call } } else { // Base case size += 1; }
return size; } }
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