Question

In: Computer Science

To implement a recursive algorithm to calculates the total directory size (in bytes) of the portion...

To implement a recursive algorithm to calculates the total directory size (in bytes) of the portion of the file system rooted at the given path.

Program will rely on the following methods of the class:

  • new File(pathString) or new File(parentFile,childString)
  • file.length() returns the immediate disk usage(measured in bytes)
  • file.isDirectory() Returns true if the File instance represents a directory; false otherwise
  • file.list() Return an array of strings designating the names of all entries within the given directory

Solutions

Expert Solution

The code is :

import java.io.File;

public class FileSize
{
   static long findSize(File[] arr,int index,long size)
   {
       if(index == arr.length)
           return size;
       if(arr[index].isFile())
           size = size + arr[index].length();
       else if(arr[index].isDirectory())
           size = size + findSize(arr[index].listFiles(), 0,0);
       return (size + findSize(arr,++index,0));
   }
   public static void main(String[] args)
   {
       String path = "/home/shrutina/DSAAssingments";
       File dir = new File(path);
       long size;
      
       if(dir.exists() && dir.isDirectory())
       {   File arr[] = dir.listFiles();
           size = findSize(arr,0,0);
           System.out.println("File size is "+size);
   }
   }
}
Output:


Related Solutions

Implement function (in C programming) that calculates and returns the total size of items in a...
Implement function (in C programming) that calculates and returns the total size of items in a directory given by name. Only consider immediate contents (no need to recursively check subdirectories). Assume that appropriate header files are included (no need to specify them using #include). int dir_size(const char *name);
Python: How would I write a function that takes a directory and a size in bytes,...
Python: How would I write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size. For example, I can use this function to look for files larger than 1 Meg below my Home directory.
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. 2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. My List.txt Values 7 3 4 1 4 4 9 9 4 8 4 5 3 9 2 3 7 0 6 4 4 5 0 1 9 2 1 7...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a...
1)Think of a better way to enhance Fibonacci recursive algorithm,,, report your finding. 2) Implement a recursive function to print an array from the middle and from left to right. 3) Trace tower of Hanoi recursive algorithm for 4 discs.
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm...
Exercises a - b refer to the recursive algorithm SelectionSort (a.) In one part of algorithm SelectionSort, the index of the maximum item in a list must be found. This requires comparisons between list elements. In an n-element (unsorted) list, how many such comparisons are needed in the worst case to find the maximum element? How many such comparisons are needed in the average case? (b.) Defining the basic operation as the comparison of list elements and ignoring the amount...
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm....
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm. The following requirements should meet: a The program shall graphically prompt the user for a file. bThe program shall read the selected file which will contain 1 integer per line. c. The program shall sort the values it reads from the file from largest to smallest. d.The program shall write the values to an output file from largest to smallest in the same directory...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to...
Iterative implementation of a recursive algorithm executes faster than recursive implementation because no _____ needs to be maintained. Select one: a. recursion b. iteration c. recurrence d. stack e. array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT