Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is FileUtils.sizeOfDirectoryAsBigInteger() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

sizeOfDirectoryAsBigInteger() is a staticthe methods in Java that can be called without creating an object of the class. method of the FileUtils class that is used to get the size of a directory recursively in bytes.

How to import FileUtils

The definition of FileUtils can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
</dependency>

For other versions of the commons-io package, refer to the Maven Repository.

You can import the FileUtils class as follows:


import org.apache.commons.io.FileUtils;

Syntax

public static BigInteger sizeOfDirectoryAsBigInteger(final File directory)

Parameters

  • final File directory: This is the directory to get the size.

Return value

This method returns the size of the directory in bytes.

Code

import org.apache.commons.io.FileUtils;
import java.io.File;
public class Main{
public static void main(String[] args){
String filePath = "/Users/educative/Downloads";
File file = new File(filePath);
System.out.printf("The output of FileUtils.sizeOfDirectoryAsBigInteger(%s) is - %s", filePath,FileUtils.sizeOfDirectoryAsBigInteger(file));
System.out.println();
filePath = "1.txt";
file = new File(filePath);
System.out.printf("The output of FileUtils.sizeOfDirectoryAsBigInteger(%s) is - %s", filePath,FileUtils.sizeOfDirectoryAsBigInteger(file));
}
}

Example 1

  • Directory file path: "/Users/educative/Downloads"

The method returns the value 55464744799, indicating the size of the directory (supplied as the argument) in bytes.

Example 2

  • Directory file path: "1.txt"

The method throws an IllegalArgumentException, indicating that the file is not a directory.

Output

The output of the code will be as follows:

The output of FileUtils.sizeOfDirectoryAsBigInteger(/Users/educative/Downloads) is - 55464744799
Exception in thread "main" java.lang.IllegalArgumentException: Parameter 'directory' is not a directory: '1.txt'
	at org.apache.commons.io.FileUtils.requireDirectory(FileUtils.java:2635)
	at org.apache.commons.io.FileUtils.requireDirectoryExists(FileUtils.java:2651)
	at org.apache.commons.io.FileUtils.sizeOfDirectoryAsBigInteger(FileUtils.java:2928)
	at Main.main(Main.java:15)

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring