Why I Wrote this Program?
I had a large collection of my personal pictures and videos on many folders on my laptop. I wanted to know which folder is taking lots of spaces. In fact I wanted to compare the total sizes taken by those folders.
What this program does?
This program recurses through a folder, finds the total size occupied by the folder. For my own use, I modified it to go level down and find the sizes of individual folders at that level.
This class generates a graphical chart of total folder sizes of any given folder’s one level down subfolders.
For instance, if you have a Folder A which has B,C,D as immediate subfolders (which in turn may have many other subfolders).This class will take folder A as an input and create a graph of folder sizes of folders at level B, C, D.
Total size at level B, C and D is calculated by recursively going to all subfolders. See the diagram below.
Then using JFreeChart API, it draws simple Bar Chart (3D) to show the total folder sizes at that level.
Requirements:
– JFreeChart API (available at JFree.org)
package com.kushal.charts; /** * @Author Kushal Paudyal * www.sanjaal.com/java * Last Modified On: 2009-10-07 * * This class generates a graphical chart of total folder sizes * of any given folder's one level down subfolders. * * For instance, if you have a Folder A which has B,C,D as immediate * subfolders (which in turn may have many other subfolders).This class * will take folder A as an input and create a graph of folder sizes * of folders at level B, C, D. * * Total size at level B, C and D is calculated by recursively going to * all subfolders. */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class FolderGraph { static int totalFolderCount = 0; static 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:\\Program Files\\DbVisualizer-6.5.4"; ArrayList dataList = new ArrayList(); File myFile = new File(folder); File[] fileArray = myFile.listFiles(); for (int i = 0; i < fileArray.length; i++) { if (fileArray[i].isDirectory()) { long fileSizeByte = getFileSize(new File(fileArray[i] .getAbsolutePath())); MyFileObj obj = new MyFileObj(fileArray[i].getName(), fileSizeByte / (1024 * 1024)); dataList.add(obj); } } CategoryDataset categoryDataset = createCategoryDataset(dataList); JFreeChart chartHorizontal = create3DBarChart(categoryDataset, PlotOrientation.VERTICAL); /** Define a location to save this created file* */ String horizontalChartFileSaveLocation = "C:/temp/myFolderSizeGraph.jpg"; /** Save the chart to the file system* */ saveChart(chartHorizontal, horizontalChartFileSaveLocation); System.out.println("done"); } /** * This is a recursive method. If file is found, total file size is * calculated. If it is a folder, we recurse further. */ public static 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; } private static CategoryDataset createCategoryDataset(ArrayList myFileObjArr) { final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < myFileObjArr.size(); i++) { dataset.addValue(((MyFileObj) myFileObjArr.get(i)) .getFolderSizeTotal(), "Folder Size (MB)", ((MyFileObj) myFileObjArr.get(i)).getFolderName()); } return dataset; } private static JFreeChart create3DBarChart(CategoryDataset dataset, PlotOrientation plotOrientation) { JFreeChart chart = ChartFactory.createBarChart3D( "FolderGraph - www.sanjaal.com/java", // Chart Title "Folder Name", // Domain Axis Label "Folder Size", // Range Axis Label dataset, // Data plotOrientation, // Orientation true, // Include Legend true, // Tooltips false // Urls ); return chart; } public static void saveChart(JFreeChart chart, String fileLocation) { try { /** * This utility saves the JFreeChart as a JPEG First Parameter: * FileName Second Parameter: Chart To Save Third Parameter: Height * Of Picture Fourth Parameter: Width Of Picture */ ChartUtilities.saveChartAsJPEG(new File(fileLocation), chart, 900, 900); } catch (IOException e) { e.printStackTrace(); System.err.println("Problem occurred creating chart."); } } } class MyFileObj { String folderName; long folderSizeTotal; public MyFileObj(String folderName, long folderSizeTotal) { this.folderName = folderName; this.folderSizeTotal = folderSizeTotal; } /** * @return the folderName */ public String getFolderName() { return folderName; } /** * @param folderName * the folderName to set */ public void setFolderName(String folderName) { this.folderName = folderName; } /** * @return the folderSizeTotal */ public long getFolderSizeTotal() { return folderSizeTotal; } /** * @param folderSizeTotal * the folderSizeTotal to set */ public void setFolderSizeTotal(long folderSizeTotal) { this.folderSizeTotal = folderSizeTotal; } /* * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR * HIGH RISK ACTIVITIES. */ }
Sample output of running this program on my system:
Processing .install4j Processing doc Processing images Processing licenses Processing jdbc Processing db2 Processing derby Processing jtds Processing mimer Processing mysql Processing postgresql Processing jre Processing bin Processing client Processing new_plugin Processing lib Processing applet Processing cmm Processing deploy Processing jqs Processing ff Processing chrome Processing content Processing ie Processing ext Processing fonts Processing i386 Processing im Processing images Processing cursors Processing management Processing security Processing servicetag Processing zi Processing Africa Processing America Processing Argentina Processing Indiana Processing Kentucky Processing North_Dakota Processing Antarctica Processing Asia Processing Atlantic Processing Australia Processing Etc Processing Europe Processing Indian Processing Pacific Processing SystemV Processing lib Processing resources Processing keymaps Processing profiles Processing resources Processing wrapper Processing classes Processing com Processing onseven Processing dbvis Processing wrapper done
Originally posted 2009-10-07 10:16:01.