Java Examples - Directory Size

Advertisements


Problem Description:

How to get the size of a directory?

Solution:

Following example shows how to get the size of a directory with the help of FileUtils.sizeofDirectory(File Name) method of FileUtils class.

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

public class Main {
   public static void main(String[] args) {
      long size = FileUtils.sizeOfDirectory
      (new File("C:/Windows"));
      System.out.println("Size: " + size + " bytes");
   }
}

Result:

The above code sample will produce the following result.

Size: 2048 bytes


Advertisements
Advertisements