Get directory size

In this example we shall show you how to get the size of a directory. We are using the class that provides general file manipulation utilities. To get the size of a directory one should perform the following steps:

  • Create a String with the path to the directory.
  • Create a new File instance by converting the given pathname string into an abstract pathname.
  • Use the sizeOfDirectory(File directory) method of class. It counts the size of the directory recursively (it counts the sum of the length of all files),

as described in the code snippet below.  

package com.javacodegeeks.snippets.core;


public class GetDirectorySize {
	public static void main(String[] args) {
		// Set a string with the path of the directory
		String directory = "C:/Program Files"; 
		// Get the size of the folder
		long size = FileUtils.sizeOfDirectory(new File(directory));

		// Print the result
		System.out.println("The size of directory " + directory + " is " + size + " bytes");


The size of directory C:/Program Files is 4073913382 bytes

This was an example of how to get the size of a directory in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button