code: public static long getDirectorySize(File directory) {
          if (!directory.exists()) {
              throw new IllegalArgumentException(directory + " does not exist");
          }
  
          if (!directory.isDirectory()) {
              throw new IllegalArgumentException(directory + " is not a directory");
          }
          long size = 0;
          File[] files = directory.listFiles();
          if (files == null) {
              // null if security restricted
              return 0L;
          }
          for (int i = 0; i < files.length; i++) {
              File file = files[i];
  
              if (file.isDirectory()) {
                  size += getDirectorySize(file);
              } else {
                  size += file.length();
              }
          }
          return size;
      }
docs: Counts the size of a directory recursively (sum of the length of all files).
  
  @param directory directory to inspect, must not be null
  @return size of directory in bytes, 0 if directory is security restricted