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

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...
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
6 C++ Questions: 19. Assuming an int is size 4 bytes, what is the size in...
6 C++ Questions: 19. Assuming an int is size 4 bytes, what is the size in memory of the below array: int cards[10] = {7, 4, 7, 5, 7}; 20. In the array from question 19, what is the value of the below elements: cards[1]; cards[8]; Assume you have an array with 128 integers which is sorted from lowest to highest. 21a. You are searching for a value which is contained in the array using the binary search algorithm. What...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT