Sunday 13, Apr 2025
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

Related Posts:

  • How to delete directory using Java boolean java.io.File.delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the java.nio.file.Fil… Read More
  • 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… Read More
  • Java Directory Examples How to Create Directory How to retrieve all the files from Directory How to search file from directory How to retrieve all the files from Directory using File.listFiles() How to retrieve all the directory names How to retr… Read More
  • How to check directory or file is hidden using Java boolean java.io.File.isHidden() Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name beg… Read More
  • How to find parent directory of file using JavaString java.io.File.getParent()Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. The parent of an abstract pathname consists of the pathname's prefix, i… Read More
  • 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