Advertisement
risbah

Directory Size

Jan 28th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. package recursion;
  2.  
  3. import java.io.File;
  4.  
  5. public class DirectorySize {
  6.  
  7.     public static void main(String[] args) {
  8.         // Find the size of directory (sum of the sizes of all files in the directory
  9.         // you can test the program here, I didn't write the code to test it, but it works I think
  10.     }
  11.    
  12.     public static long getSize(File file) {
  13.         long size = 0;
  14.         if(file.isDirectory()) {
  15.             File[] files = file.listFiles();
  16.             for(int index=0; files != null && index<files.length; index++) {
  17.                 size += getSize(files[index]);
  18.             }
  19.         }
  20.         else {
  21.             size+=file.length();
  22.         }
  23.         return size;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement