728x90 AdSpace

Wednesday, 24 May 2017

How to find the size of directory using FileUtils

public static long sizeOfDirectory(File directory)
Counts the size of a directory recursively (sum of the length of all files). Note that overflow is not detected, and the return value may be negative if overflow occurs. See sizeOfDirectoryAsBigInteger(File) for an alternative method that does not overflow.

Parameters:
directory - directory to inspect, must not be null

Returns:
size of directory in bytes, 0 if directory is security restricted, a negative number when the real total is greater than Long.MAX_VALUE.

Throws:
NullPointerException - if the directory is null

DirectorySize.java
package com.javatutorialcorner.directory;

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

public class DirectorySize {

 public static void main(String[] args) {
   long sizeInBytes = FileUtils.sizeOfDirectory(new File("C:/JTC"));
       System.out.println("Directory Size in Bytes: " + sizeInBytes + " bytes");
 }

}
Output
Directory Size in Bytes: 240 bytes
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to find the size of directory using FileUtils Rating: 5 Reviewed By: Annamalai Thangaraj