The following piece of java code demonstrates how we can calculate the size of a given folder using Java Application. This uses a recursive technique. We start with a root directory, and start listing the files/folder. If a file is found, we calculate its size and sum of total size and also increase the total number of files counted so far, if it is a directory, we further do the recursion.
This program has been fully compiled and tested to find the folder size in Java.
/**
* @author Kushal Paudyal
* www.sanjaal.com/java
* Last Modified On: 30th August 2008
*/
package com.kushal.utilities;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class TopLevelFolderSize {
int totalFolderCount=0;
int totalFileCount=0;
public static void main(String args [])
{
/**
* Define your folder here. This is the folder whose size
* statistics you want to find out.
*/
String folder = "C:/Users/kushal/Desktop/tempKushal";
try{
DecimalFormat fmt =new DecimalFormat("#.##");
TopLevelFolderSize fg=new TopLevelFolderSize();
/**Calculating the file size. By default size in long
* is returned.
*/
long fileSizeByte=fg.getFileSize(new File(folder));
/**
* Formatting the long value to calculate size in
* different units KB, MB and GB
*/
double fileSizeKB=Double.valueOf(fmt.format(fileSizeByte /1024));
double fileSizeMB=Double.valueOf(fmt.format(fileSizeByte /(1024*1024)));
double fileSizeGB=Double.valueOf(fmt.format(fileSizeByte /(1024*1024*1024)));
/**Printing the statistics**/
System.out.println("\n\n##############--Folder Statistics--#################");
System.out.println("Total Folder Size: ["+fileSizeByte+" Bytes] \n\t\t["
+fileSizeKB+" KB] \n\t\t["
+fileSizeMB+" MB] \n\t\t["
+fileSizeGB+" GB]");
System.out.println("Total Number of Folders: "+fg.getTotalFolderCount());
System.out.println("Total Number of Files: "+fg.getTotalFileCount());
System.out.println("##########--End Of Folder Statistics--##############");
}catch (Exception e)
{
System.out.println("Exception Occurred: "+e.getMessage());
}
}
/**
* This is a recursive method.
* If file is found, total file size is calculated.
* If it is a folder, we recurse further.
*/
public long getFileSize(File folder) {
totalFolderCount++; //Counting the total folders
System.out.println("Processing " + 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 {
totalFileCount++; //Counting the total files
foldersize += filelist[i].length();
}
}
return foldersize;
}
/**
* @return the totalFolderCount
*/
public int getTotalFolderCount() {
return totalFolderCount;
}
/**
* @return the totalFileCount
*/
public int getTotalFileCount() {
return totalFileCount;
}
}
———————————————————————
SAMPLE OUTPUT:
———————————————————————
Processing tempKushal
Processing cute pictures
Processing image col
Processing Moline Tornado
Processing send
Processing Party At Prabhakars
Processing images
##############–Folder Statistics–#################
Total Folder Size: [151850658 Bytes]
[148291.0 KB]
[144.0 MB]
[0.0 GB]
Total Number of Folders: 7
Total Number of Files: 154
##########–End Of Folder Statistics–##############
Originally posted 2008-08-30 13:47:00.
Thank you Jamie for your optimized solution.
This approach uses less memory:
public static class SizeCounter implements FileFilter
{
private long total = 0;
public SizeCounter(){};
public boolean accept(File pathname) {
if ( pathname.isFile()) {
total+=pathname.length();
} else {
pathname.listFiles(this);
}
return false;
}
public long getTotal()
{
return total;
}
}
private static long getFileOrDirectorySize(File file) {
SizeCounter counter = new SizeCounter();
file.listFiles(counter);
return counter.getTotal();
}
Pingback: Sanjaal.com » Latest Updates