code: public static long sizeOfDirectory(File directory) {
      if (!directory.exists()) {
        String message = directory + " does not exist";
        throw new IllegalArgumentException(message);
      }
  
      if (!directory.isDirectory()) {
        String message = directory + " is not a directory";
        throw new IllegalArgumentException(message);
      }
  
      long size = 0;
  
      File[] files = directory.listFiles();
      if (files == null) {  // null if security restricted
        return 0L;
      }
      for (File file : files) {
        size += sizeOf(file);
      }
  
      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 <code>null</code>
  @return size of directory in bytes, 0 if directory is security restricted
  @throws NullPointerException if the directory is <code>null</code>