Android File Size get sizeOf(File file)

Here you can find the source of sizeOf(File file)

Description

size Of

License

Apache License

Declaration

public static long sizeOf(File file) 

Method Source Code

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

import java.io.File;

public class Main {
    public static long sizeOf(File file) {

        if (!file.exists()) {
            throw new IllegalArgumentException(file + " does not exist");
        }//from w w  w  .  ja v a  2  s .co m

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

    }

    public static long sizeOfDirectory(File directory) {
        if (!directory.exists()) {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        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;
    }
}

Related

  1. getFileSize(File file)
  2. getFileSize(File f)
  3. getFileSizes(File f)
  4. fileLength(String filePath)