How To Find Files or Directories Current Size(Bytes,KB,MB,GB,TB...) In Filehandling In Java.

How To Find Files or Directories Current Size(Bytes,KB,MB,GB,TB...) In Filehandling In Java.

In this article we will see how to find any files or folder / directories size (Bytes,KB,MB,GB,TB,PB,EB,ZB,YB) in filehandling concept in java. For that we will use three File class method 1)listFiles() , 2)length() , 3)isFile().

1. Use of listFiles() method in File class :

    Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
    Return Type : File[] (File Array).

2. Use of length() method in File class :

    Returns the length of the file denoted by this abstract pathname.
    Return Type : long.

3. Use of isFile() method in File class :

    Tests whether the file denoted by this abstract pathname is a normal file
    Return Type : Boolean.

We will use one more method in Math class is Math.round().

4. Use of Math.round() method in Math class :

    Returns the closest long or int to the argument, with ties rounding to positive infinity.

Example for finding size of any particular files and Directories :

package gsh.java.programs;
import java.io.File;
class findSize {
    public void findFileFolderSize(File file) {
        float bytes = file.length();
        float kilobytes = Math.round((bytes / 1024));
        float megabytes = Math.round((kilobytes / 1024));
        float gigabytes = Math.round((megabytes / 1024));
        float terabytes = Math.round((gigabytes / 1024));
        float petabytes = Math.round((terabytes / 1024));
        float exabytes = Math.round((terabytes / 1024));
        float zettabytes = Math.round((exabytes / 1024));
        float yottabytes = Math.round((zettabytes / 1024));

        System.out.printf("%,.0f Bytes%n", bytes);
        System.out.printf("%.1f KB", kilobytes);
        System.out.printf("\n%.1f MB", megabytes);
        System.out.printf("\n%.1f GB", gigabytes);
        System.out.printf("\n%.1f TB", terabytes);
        System.out.printf("\n%.1f PB", petabytes);
        System.out.printf("\n%.1f EB", exabytes);
        System.out.printf("\n%,.1f ZB", zettabytes);
        System.out.printf("\n%.1f YB", yottabytes);
    }
}
public long getDirectorySizeLegacy(File dir) {
        long length = 0;
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()){
                    length += file.length();
                }
                else{
                    length += getDirectorySizeLegacy(file);
                }
            }
        }
        return length;
 }
public class FindSizeDemo {
    public static void main(String[] args) {
        File file = new File("D:\\fileHeading");
        try {
            file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        findSize obj = new findSize();
        File[] dir = file.listFiles();
        if (file.isFile()) {
            obj.findFileFolderSize(file);
        }
        else{
            float bytes = obj.getDirectorySizeLegacy(file);
            float kb =bytes/1024;
            float mb = kb/1024;
            float gb = mb/1024;
            float tb = gb/1024;
            float pb = tb/1024;
            float eb = pb/1024;
            float zb = eb/1024;
            float yb = zb/1024;

            System.out.printf("%,.0f Bytes%n",bytes);
            System.out.printf("%,.1f KB", kb);
            System.out.printf("\n%,.1f MB",mb);
            System.out.printf("\n%,.1f GB", gb);
            System.out.printf("\n%,.1f TB",tb);
            System.out.printf("\n%,.1f PB",pb);
            System.out.printf("\n%,.1f EB",eb);
            System.out.printf("\n%,.1f ZB",zb);
            System.out.printf("\n%,.1f YB",yb);
        }
    }
    }

Output:

3,624,924,928 Bytes
3,539,965.75 KB
3,457 MB
3.38 GB //Directories size gives you to rounded size. For example your directroies size is 3.3796765 GB and will give you to 3.38 GB.
0 TB
0 PB
0 EB
0 ZB
0 YB

Post a Comment

0 Comments