Android Directory Size Get sizeOfDirectory(File directory)

Here you can find the source of sizeOfDirectory(File directory)

Description

size Of Directory

License

Apache License

Declaration

public static long sizeOfDirectory(File directory) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    public static long sizeOfDirectory(File directory) {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }//from   w ww .  j  a va  2s.c  o m

        if (!directory.isDirectory()) {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        long size = 0;

        File[] files = directory.listFiles();
        if (files == null) { // null if security restricted
            return 0L;
        }
        for (File file : files) {
            size += sizeOf(file);
        }

        return size;
    }

    public static long sizeOf(File file) {

        if (!file.exists()) {
            throw new IllegalArgumentException(file + " does not exist");
        }

        if (file.isDirectory()) {
            return sizeOfDirectory(file);
        } else {
            return file.length();
        }

    }
}

Related

  1. folderSize(File directory)
  2. getDirLength(File dir)
  3. sizeOf(File spec)
  4. getDirectorySize(File f)