java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to calculate directory size?

import java.io.File;
import org.apache.commons.io.FileUtils;

public class DirectorySizeSample
{
    public static void main(String[] args)
    {
        // Using FileUtils.sizeOfDirectory() we can calculate
        // the size of a directory including it sub directory
        long size = FileUtils.sizeOfDirectory(new File("C:/Windows"));

        System.out.println("Size: " + size + " bytes");
    }
}