import javax.swing.*;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class FindFolderSize {
int totalFolderCount=0;
int totalFileCount=0;
int totalFolder=0;
int totalFile=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 = "D:/JAVA Content/Coding";
String folder=JOptionPane.showInputDialog(null,"Type input folder path");
try{
DecimalFormat fmt =new DecimalFormat("#.##");
FindFolderSize fg=new FindFolderSize();
/**Calculating <span id="IL_AD10" class="IL_AD">the file</span> 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());
}
}
/**
* @return the totalFolderCount
*/
public int getTotalFolderCount() {
return totalFolderCount;
}
/**
* @return the totalFileCount
*/
public int getTotalFileCount() {
return totalFileCount;
}
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;
}
}