How to calculate the size of folder using Java?



You can get the size of a directory with the help of FileUtils.sizeofDirectory(File Name) method of FileUtils class.

Example

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");
   }
}

Output

Size: 2048 bytes
Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest


Advertisements