Tuesday, November 8, 2016

Extract files & folder along with their sizes using Java

In this tutorial we will make use of File class to extract all files & folders within any directory on your computer. For this exercise we will extract the files and folders along with their sizes from Desktop.

Git Location:
https://github.com/csanuragjain/extra/tree/master/FileFolderExtractor

Program Explanation:

Main Method:
      public static void main(String[] args) {  
           String path=System.getProperty("user.home") + "/Desktop";  
           File f= new File(path);  
           File[] desktopProjects=f.listFiles();  
           for(File file:desktopProjects)  
           {  
                if(file.isDirectory())  
                {  
                     System.out.println("Directory: "+file.getName()+":"+(folderSize(file)/1000)+"KB");  
                }  
                else  
                {  
                     System.out.println("File: "+file.getName()+":"+(file.length()/1000)+"KB");  
                }  
           }  
      }  

How it works:

1) We call System.getProperty("user.home") which returns the current home for the logged in user. In this example we extract the files and folders from Desktop. Since desktop folder is inside the user home directory so we append Desktop with user home to get Desktop path
2) We point the File class to Desktop path
3) f.listFiles() returns all the files inside the Desktop path which gets stored in desktopProjects variable.
4) Now the values returned by listFiles function may be file or directory so we check this by calling the function isDirectory (return true if returned value is directory otherwise false).
5) For every file found we use file.getName to retrieve the name of file and file.length to retrieve the file length. Since the file length is returned in bytes so we convert this to KB by dividing by 1000
6) Now for every directory found we use getName to get the directory name. But for obtaining the folder size we cannot use file.length since it wont work so we made a new function for obtaining the folder length which is folderSize

folderSize function:
      public static long folderSize(File directory) {  
        long length = 0;  
        for (File file : directory.listFiles()) {  
          if (file.isFile())  
            length += file.length();  
          else  
            length += folderSize(file);  
        }  
        return length;  
      }  

How it works:

1) Since we cannot find length of folder so we iterate through each file in folder and extract size of all files within.
2) First we call listFiles to get all files within the directory.
3) If the returned from step2 is file then we update the length
4) If the returned from step2 is directory then we again recursively call this function so that the size of this subdirectory gets calculated.

Full Code:
 package com.cooltrickshome;  
 import java.io.File;  
 public class FileFolderExtractor {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           String path=System.getProperty("user.home") + "/Desktop";  
           File f= new File(path);  
           File[] desktopProjects=f.listFiles();  
           for(File file:desktopProjects)  
           {  
                if(file.isDirectory())  
                {  
                     System.out.println("Directory: "+file.getName()+":"+(folderSize(file)/1000)+"KB");  
                }  
                else  
                {  
                     System.out.println("File: "+file.getName()+":"+(file.length()/1000)+"KB");  
                }  
           }  
      }  
      public static long folderSize(File directory) {  
        long length = 0;  
        for (File file : directory.listFiles()) {  
          if (file.isFile())  
            length += file.length();  
          else  
            length += folderSize(file);  
        }  
        return length;  
      }  
 }  

Hope this helps :)

No comments:

Post a Comment