Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

In Java how to Get File or Directory Size Programmatically?

In Java how to Get File or Directory Size Programmatically?

java.io.File is my favorite utility which deals with lots of File related to functionalities. I started dealing with Java and File utils more and more in my initial coding days. It’s always fun to perform number of different operations using java.io.File.

In Java, you can use the File.length() method to get the file size in bytes.

Example:

  • Create file CrunchifyFindFileFolderSize.java.
  • Copy below code and save file.
package crunchify.com.tutorials;

import java.io.File;
import java.util.Objects;

/**
 * @author Crunchify.com
 * In Java how to Get File or Directory Size Programmatically?
 */

public class CrunchifyFindFileFolderSize {

        public static void main(String[] args) {
                File crunchifyFile = new File("/Users/app/Downloads/index.php");
                long crunchifySize = 0;
                crunchifySize = getFileFolderSize(crunchifyFile);

                double sizeMB = (double) crunchifySize / 1024 / 1024;
                String crunchifyString = " MB";
                if (sizeMB < 1) {
                        sizeMB = (double) crunchifySize / 1024;
                        crunchifyString = " KB";
                }
                
                // getName(): Returns the name of the file or directory denoted by this abstract pathname.
                // This is just the last name in the pathname's name sequence.
                // If the pathname's name sequence is empty, then the empty string is returned.
                System.out.println(crunchifyFile.getName() + " : " + sizeMB + crunchifyString);
        }

        // Custom function to get File and FolderSize
        public static long getFileFolderSize(File crunchifyDirectory) {
                long crunchifySize = 0;
                if (crunchifyDirectory.isDirectory()) {
                        
                        // listFiles(): Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
                        // If this abstract pathname does not denote a directory, then this method returns null.
                        // Otherwise an array of File objects is returned, one for each file or directory in the directory.
                        // Pathnames denoting the directory itself and the directory's parent directory are not included in the result.
                        // Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute;
                        // if this pathname is relative then each resulting pathname will be relative to the same directory.
                        for (File crunchifyFile : Objects.requireNonNull(crunchifyDirectory.listFiles())) {
                                
                                // isFile(): Tests whether the file denoted by this abstract pathname is a normal file.
                                // A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria.
                                if (crunchifyFile.isFile()) {
                                        
                                        // length(): Returns the length of the file denoted by this abstract pathname.
                                        // The return value is unspecified if this pathname denotes a directory.
                                        crunchifySize += crunchifyFile.length();
                                } else
                                        crunchifySize += getFileFolderSize(crunchifyFile);
                        }
                } else if (crunchifyDirectory.isFile()) {
                        crunchifySize += crunchifyDirectory.length();
                }
                return crunchifySize;
        }
}

Just run above program as a Java Application and you will see result as below:

index.php : 0.029296875 KB

Process finished with exit code 0

Let me know if you face any issue running this program.

The post In Java how to Get File or Directory Size Programmatically? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires