Android Folder Size Get getSize(final File path)

Here you can find the source of getSize(final File path)

Description

Gets the size of the path.

License

Open Source License

Parameter

Parameter Description
path path

Return

path size in bytes

Declaration

public static long getSize(final File path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**/*ww w  . j  av a  2  s  . co  m*/
     * Gets the size of the path.
     * <p>
     * If the path is a directory the size is the sum of the size of all the files in the
     * directory and sub-directories. If the path is a file, the size is the size of the
     * file.
     * </p>
     * 
     * @param path path
     * @return path size in bytes
     */
    public static long getSize(final File path) {
        long size = 0;
        if (path.isFile()) {
            size = path.length();
        } else {
            File[] subFiles = path.listFiles();

            for (File file : subFiles) {
                if (file.isFile()) {
                    size += file.length();
                } else {
                    size += getSize(file);
                }
            }
        }

        return size;
    }
}

Related

  1. getFolderSize(java.io.File file)
  2. getFolderSize(File file)