Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Java get Folder Size

Related Tutorials


 
 

Share on Google+Share on Google+

Java get Folder Size

Advertisement
In this section, you will learn how to get the size of specified folder.

Java get Folder Size

     

In this section, you will learn how to get the size of specified folder. 

In the given example, we are going to show you the size of specified folder and also the number of files and folders that are presented in the specified folder. We have create a method getFileSize() in order to obtain the size of folder. The totalFolder++ counts the total folders and totalFile++ counts the total files.

folder.getName()-This method returns the folder name.

folder.listFiles()- This method returns an array of files presented in the specified folder.

filelist[i].length() - This method returns the length of files presented in the specified folder. 

Following code will calculate the size of folder in bytes.

long fileSizeByte=size.getFileSize(new File(folder))

Here is the code of GetFolderSize.java

import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class GetFolderSize {

  int totalFolder=0;
  int totalFile=0;

  public static void main(String args [])
  {  
  String folder = "C:/GetExamples";
  try{
  GetFolderSize size=new GetFolderSize();
  long fileSizeByte=size.getFileSize(new File(folder));
  System.out.println("Folder Size: "+fileSizeByte+" Bytes" );
  System.out.println("Total Number of Folders: "+size.getTotalFolder());
  System.out.println("Total Number of Files: "+size.getTotalFile());
  }catch (Exception e)
  {}
  }
  public long getFileSize(File folder) {
  totalFolder++; 
  System.out.println("Folder: " + folder.getName());
  long foldersize = 0;

  File[] filelist = folder.listFiles();
  for (int i = 0; i < filelist.length; i++) {
  if (filelist[i].isDirectory()) {
  foldersize += getFileSize(filelist[i]);
  else {
  totalFile++;
  foldersize += filelist[i].length();
  }
  }
  return foldersize;
  }
  public int getTotalFolder() {
  return totalFolder;
  }
  public int getTotalFile() {
  return totalFile;
  }
}

Output will be displayed as:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 1, 2008

Related Tutorials

Discuss: Java get Folder Size   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
sairam
November 14, 2011
java

this site is very useful
Joshua Vannoster
February 28, 2012
Question on the sourcecode

what is the line GetFolderSize size=new GetFolderSize(); for? it's not making sense to, since there isn't even a constructor.
Navdeep Singh
May 21, 2012
How to skip loop of recursion ?

Thanks for this helpful program & tip. But what can we do if we got situations like un-accessible folder like System Volume Information from Windows XP or when we get corrupt file or folder. How can we skip a loop of recursion in this kind of situation?
DMCA.com