Created
September 12, 2016 15:43
-
-
Save hoanbka/7ce5db8707ae293b64f117afa657cdb1 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.ArrayList; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class SizeCalculationUsingQueue { | |
private static Queue<File> queue = new LinkedList<>(); | |
static long size = 0; | |
public static long getSize(File dir) { | |
for (File file : dir.listFiles()) { | |
queue.add(file); | |
} | |
while (true) { | |
ArrayList<File> subFiles = new ArrayList<>(); | |
Iterator<File> iterator = queue.iterator(); | |
while (iterator.hasNext()) { | |
File file = iterator.next(); | |
if (file.isFile()) { | |
size = size + file.length(); | |
iterator.remove(); | |
} else { | |
for (File f : file.listFiles()) { | |
subFiles.add(f); | |
} | |
iterator.remove(); | |
} | |
} | |
queue.addAll(subFiles); | |
if (queue.isEmpty()) { | |
break; | |
} | |
} | |
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