Problem A.3 Programming Language Java Write a program that prompts the user to e
ID: 3570142 • Letter: P
Question
Problem A.3 Programming Language Java
Write a program that prompts the user to enter a directory and displays the number of files in that directory. Your result should also include all the files from each subdirectory. Do not count folders and subfolders as files. Use an appropriate try and catch statement to handle the case when the given path does not exist. Hint: Use recursion for subdirectories (see chapter 18)
Example output:
Analysis (2 points):
Describe the problem including input and output in your own words.
Design (2 points):
Describe the major steps for solving the problem. How do you use recursion to solve this problem?
Explanation / Answer
//java program that prompts user to enter a valid directory
//to display the files in the total files in the directories and
//subdirectories .
//ListFiles.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ListFiles
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Â
     Scanner reader=new Scanner(System.in);
     String directory;
     int totalFiles=0;
     File currentDir =null;
     //Prompts user to enter a valid directory name
     do
     {
        //read name of the directory
        System.out.println("Enter directory ");
        directory=reader.nextLine();
        // current directory
        currentDir = new File(directory);
        if(!currentDir.exists())
        {
           System.out.println("Invalid Directory:");
           System.out.println("Enter a valid directory");
        }
       Â
     }while(!currentDir.exists());
     //call the metod
     totalFiles=printFiles(currentDir);
     System.out.println("Total Number of Files : "+totalFiles);
  }
 Â
  //The method printFiles that accepts an directory name
  //and prints the all files in the directories and subdirectories
  //recursively.
  public static int printFiles(File dir)
  {
     int count=0;
     try
     {
        File[] files = dir.listFiles();
        for (File file : files)
        {
           //calls the subdirectory for files
           if (file.isDirectory())
           {
              //count the files in the current directory
              count=count+printFiles(file);
           }
           else
           {
             Â
              System.out.println(" File:" + file.getCanonicalPath());
             Â
              //increment the count if the file is not directory.
              count++;
           }
        }
     }
     //catch the IO exception.
     catch (IOException e)
     {
        System.out.println(e.getMessage());
     }
     //return total files in the directory.
     return count;
  }
}
-----------------------------------------------------------------------------------------------
Sample output:
Enter directory
E:kids_imagesFiles
Invalid Directory:
Enter a valid directory
Enter directory
E:kids_images
File:E:kids_images937475_814053508636066_35974530819673309
69_n.jpg
  File:E:kids_images¡95xcitefun-kidspartywear7.jpg
 Â
File:E:kids_imagesdeep-wine-kids-traditional-saree-800x1100.jpg
  File:E:kids_imagesDesigner-Indian-Kids-Wear.jpg
 Â
File:E:kids_imagesdiscuss-divorce-with-children-houston-family-law-001
.jpg
  File:E:kids_imagesimage13569531.jpg
  File:E:kids_imagesimages.jpeg
  File:E:kids_imagesimages5646.jpeg
  File:E:kids_imagesindian_kids_fashion_15.jpg
  File:E:kids_imagesindian_kids_fashion_19.jpg
  File:E:kids_imagesindian_kids_fashion_26.jpg
  File:E:kids_imagesindian_kids_fashion_31.jpg
  File:E:kids_imagesjiya.jpg
  File:E:kids_imageskids vaddanam and kasu haram.jpg
  File:E:kids_imageskids_ind_2.29002106_std.jpg
 Â
File:E:kids_imagesLatest-Beautiful-Kids-Indian-Saree-Style-2012.jpg
  File:E:kids_imagesLight-Aqua-Blue-Net-Kids-Saree.jpg
 Â
File:E:kids_imagesorang-color-Indian-wedding-wear-dress-2014.jpg
  File:E:kids_imagesThumbs.db
 Â
File:E:kids_imagesTraditional-fashion-for-kids-indian-festival-ganesh-cha
turthi.jpg
  File:E:kids_imagesu08twetiuow.jpg
  File:E:kids_imagesUSJ72_large.jpg
Total Number of Files : 22
Hoep this helps you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.