Get directory Size by a pattern - Android java.io

Android examples for java.io:Directory

Description

Get directory Size by a pattern

Demo Code

import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

public class Main {

  public static long directorySize(File sourceLocation, Pattern pattern) throws IOException {
    long size = 0;
    if (sourceLocation == null || !sourceLocation.exists()) {
      return size;
    }/*from w w  w  . j  ava 2s. com*/
    if (sourceLocation.isDirectory()) {
      String[] children = sourceLocation.list();
      for (int i = 0; i < children.length; i++) {
        size += directorySize(new File(sourceLocation, children[i]), pattern);
      }
      return size;
    } else if ((pattern == null || pattern.matcher(sourceLocation.getName()).matches())) {
      return sourceLocation.length();
    } else {
      return 0;
    }
  }
}

Related Tutorials