Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created September 12, 2016 15:43
Show Gist options
  • Save hoanbka/26c20e97efae363d6255d3f201e571e2 to your computer and use it in GitHub Desktop.
Save hoanbka/26c20e97efae363d6255d3f201e571e2 to your computer and use it in GitHub Desktop.
Calculate the size of the directory using queue
package sizecalculation;
import java.io.File;
import java.util.*;
public class SizeCalculationUsingQueue2 {
private static Queue<File> queue = new LinkedList<>();
static long size = 0;
public static long getSize(File dir) {
queue.addAll(Arrays.asList(dir.listFiles()));
File file;
while ((file = queue.poll()) != null) {
if (file.isFile()) {
size += file.length();
} else {
queue.addAll(Arrays.asList(file.listFiles()));
}
}
return size;
}
public static void main(String[] args) {
File dir = new File("D:\\java\\TaiLieu\\__MACOSX");
System.out.println("Total size of directory = " + getSize(dir) + " bytes");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment