Skip to content

Instantly share code, notes, and snippets.

@toboqus
Last active January 4, 2016 08:18
Show Gist options
  • Save toboqus/ab74343c9ee46cd61cc5 to your computer and use it in GitHub Desktop.
Save toboqus/ab74343c9ee46cd61cc5 to your computer and use it in GitHub Desktop.
counting number of files within a given directory (Breadth first)
public int count(File root){
if(root == null || !root.exists()){
System.out.println("ERROR: The source drive/directory does not exist.");
return -1;
}
Queue<File> files = new Queue<>();
files.push(root);
int sum = 0;
while(!files.empty()){
File tmp = files.pop();
if(tmp.isFile()){
sum++;
}else if(tmp.isDirectory()){
for(File f : tmp.listFiles()){
files.push(f);
}
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment