We are moved to new domain
Click -> www.ehowtonow.com
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

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: eHowToNow