How to calculate the size of folder using Java?

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 12-Jan-2018 06:42:48
Advertisements