Created
September 12, 2016 15:43
-
-
Save hoanbka/26c20e97efae363d6255d3f201e571e2 to your computer and use it in GitHub Desktop.
Calculate the size of the directory using queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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