compute the size of one folder in long value by adding file length together - Android java.io

Android examples for java.io:Folder

Description

compute the size of one folder in long value by adding file length together

Demo Code

import java.io.File;

public class Main {

  /**//w ww  .  ja va2 s  .  c  o m
   * compute the size of one folder
   * 
   * @param dir
   * @return the byte length for the folder
   */
  public static long computeFolderSize(final File dir) {
    if (dir == null) {
      return 0;
    }
    long dirSize = 0;
    final File[] files = dir.listFiles();
    if (null != files) {
      for (int i = 0; i < files.length; i++) {
        final File file = files[i];
        if (file.isFile()) {
          dirSize += file.length();
        } else if (file.isDirectory()) {
          dirSize += file.length();
          dirSize += computeFolderSize(file);
        }
      }
    }
    return dirSize;
  }
}

Related Tutorials