Here you can find the source of sizeOfDirectory(File directory)
public static long sizeOfDirectory(File directory)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { public static long sizeOfDirectory(File directory) { if (!directory.exists()) { String message = directory + " does not exist"; throw new IllegalArgumentException(message); }//from w ww . j a va 2s.c o m 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; } public static long sizeOf(File file) { if (!file.exists()) { throw new IllegalArgumentException(file + " does not exist"); } if (file.isDirectory()) { return sizeOfDirectory(file); } else { return file.length(); } } }